Template problems: No matching function for call
- by Nick Sweet
I'm trying to create a template class, and when I define a non-member template function, I get the "No matching function for call to randvec()" error.
I have a template class defined as:
template <class T>
class Vector {
T x, y, z;
public:
//constructors
Vector();
Vector(const T& x, const T& y, const T& z);
Vector(const Vector& u);
//accessors
T getx() const;
T gety() const;
T getz() const;
//mutators
void setx(const T& x);
void sety(const T& y);
void setz(const T& z);
//operations
void operator-();
Vector plus(const Vector& v);
Vector minus(const Vector& v);
Vector cross(const Vector& v);
T dot(const Vector& v);
void times(const T& s);
T length() const;
//Vector<T>& randvec();
//operators
Vector& operator=(const Vector& rhs);
friend std::ostream& operator<< <T>(std::ostream&, const Vector<T>&);
};
and the function in question, which I've defined after all those functions above, is:
//random Vector
template <class T>
Vector<double>& randvec()
{
const int min=-10, max=10;
Vector<double>* r = new Vector<double>;
int randx, randy, randz, temp;
const int bucket_size = RAND_MAX/(max-min +1);
temp = rand(); //voodoo hackery
do randx = (rand()/bucket_size)+min;
while (randx < min || randx > max);
r->setx(randx);
do randy = (rand()/bucket_size)+min;
while (randy < min || randy > max);
r->sety(randy);
do randz = (rand()/bucket_size)+min;
while (randz < min || randz > max);
r->setz(randz);
return *r;
}
Yet, every time I call it in my main function using a line like:
Vector<double> a(randvec());
I get that error. However, if I remove the template and define it using 'double' instead of 'T', the call to randvec() works perfectly. Why doesn't it recognize randvec()?
P.S. Don't mind the bit labeled voodoo hackery - this is just a cheap hack so that I can get around another problem I encountered.