Hello All
Strange problem, I just do not understand what is going on... basically, I have a series of template functions and template classes. I am trying to use the template functions within template classes to help me with some of the basic nitty gritty stuff like copying vectors.
I have a template class called state, the real version has many more members, but the one here has a vector<T> member. I am trying to write an assignment operator that can copy the from another state with a different template parameter. This assignment operator calls a template<class T,class U> copyVector function. Yet, when I compile it seems that the compiler cannot find the copyVector function. I don't know what I need to do to get this to bind.
Here is some sample code:
Code:
#include <vector>
template<class T,class U>
void copyVector(std::vector<T, std::allocator<T> > &vct, std::vector<U, std::allocator<U> > &asg)
{
vct.resize(asg.size());
for(unsigned long i;i<asg.size();i++)
vct[i]=asg[i];
return;
}
template<class T>
struct state
{
std::vector<T> data;
template<class U> state<T>& operator=(const state<U> &asg)
{
copyVector<T,U>(this->data,asg.data);
return *this;
}
};
struct cont
{
double value;
cont& operator=(const cont &asg)
{
this->value=asg.value;
return *this;
}
};
int main()
{
state<double> base;
state<cont> copy;
copy=base;
}
These are the error messages I get:
Code:
test.cpp: In member function ‘state<T>& state<T>::operator=(const state<U>&) [with U = double, T = cont]’:
test.cpp:35: instantiated from here
test.cpp:20: error: no matching function for call to ‘copyVector(std::vector<cont, std::allocator<cont> >&, const std::vector<double, std::allocator<double> >&)’