I wrote a quick method to convert std::vectors from one type to another:
template<class A, class B>
vector<B> ConvertSTDVector_AToB(vector<A> vector)
{
vector<B> converted_vector;
for(unsigned int i= 0; i< vector.size(); i++)
converted_vector.push_back(vector[i]);
return converted_vector;
}
But the compiler errors with "error C2275: 'B' : illegal use of this type as an expression" on the line after the opening bracket. At first I thought somehow 'B' was defined elsewhere, but changing both template type names results in the same error. Then I thought that something strange was going on with the types. But even making both template parameters ints doesn't change anything.
I can't for the life of me see what's wrong with this method. (Although I feel like I might just be blind to something obvious, at this point).
Simply rename the parameter name. Its name hides the std::vector name.
Or write the erroneous line the following way
that is use the elaborated type name for std::vector that to distinguish it from object (parameter) vector.
The code of the function can be written in different ways. For example
pr
ans so on.
You don't really have to do such a function, instead all you have to do is to use
std::copy
provided that the objects you want to convert are convertible to each other by providing an overloaded conversion operator. Just like the example below:DEMO