templated std::vector variable as an argument by reference problem
sorry i am too sure how to write a more explicit subject but this code
doesn't compile for the type string and I am not sure why (and I am
not sure either how to describe the problem but by looking at the
program you should understand what I am trying to do easily, which is
convert an array of string to an array of another type, either string,
float or interger.)
I am not sure what I am trying to do is legal. Obvisouly it doesn't
seem to be as the compiler complains but I wonder if there's a way i
can get it to work ?
Thanks for your help.
-mark
template<typename T>
void GetArray( std::vector<T> &array )
{
std::vector<std::string> strArray;
strArray.push_back( "test1" );
strArray.push_back( "test2" );
if ( typeid( std::string ) == typeid( T ) )
{
for ( size_t i = 0; i < strArray.size(); ++i )
{
// DOESN'T WORK ??? <<< REFUSE TO COMPILE IF THE NEXT LINE IS
COMMENTED OUT!
//array.push_back( strArray[i] );
}
}
std::vector<std::string> intArray;
intArray.push_back( "1" );
intArray.push_back( "2" );
if ( typeid( int ) == typeid( T ) )
{
for ( size_t i = 0; i < intArray.size(); ++i )
{
array.push_back( atoi( intArray[i].c_str() ) );
}
}
}
int main()
{
//std::vector<std::string> strArr;
//GetArray( strArr );
std::vector<int> intArr;
GetArray<int>( intArr );
return 0;
}
/////
error: no matching function for call to 'std::vector<int,
std::allocator<int> >::push_back(std::basic_string<char,
std::char_traits<char>, std::allocator<char> >&)'
/usr/include/c++/4.0.0/bits/stl_vector.h:602: note: candidates are:
void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = int,
_Alloc = std::allocator<int>]
////