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).
vector
:vector<A> vector
. Instead of naming a type, it names a function parameter. Replace withvector<A> v
, and the code should compile (demo). – Sergey Kalinichenkostd::transform
? – WhozCraigstd::copy
:). – 101010std::copy
without explicit conversion? – pmr