The following code is a minimal example of what I am trying to do. When I compiled it with
Visual C++ 2008 Express, I got the error:

main.cpp(62) : error C2677: binary '*' : no global operator found which takes type 'Point<T>' (or there is no acceptable conversion)
1> with
1> [
1> T=float
1> ]
1> main.cpp(68) : see reference to function template instantiation 'void foo<float,double>(T,T,T)' being compiled
1> with
1> [
1> T=float
1> ]

However, if I change foo so that it does not take any parameter, the error will be gone.

Could anyone tell me where I am wrong? Thanks

template<class T>
class Point {
private:
	T x, y, z;
public:
	Point(T xx, T yy, T zz) : x(xx), y(yy), z(zz) {}

	template<class U> operator Point<U> () const { return Point<U>( U(x), U(y), U(z) ); }
};

template<class T>
class Matrix{
private:
	T m[16];
public:
	Matrix(T x, T y, T z){}	
	
	Point<T> operator* (const Point<T>& p) { return Point<T>(0,0,0); } 
};

/* this does not work */
template<class T, class C>
void foo(T x, T y, T z)
{
	Point<T> o1(0,0,0);
	Matrix<C> m(C(x),C(y), C(z));
	m * o1;
}

/*but this does work
template<class T, class C>
void foo()
{
	Point<T> o1(0,0,0);
	Matrix<C> m(C(1),C(1), C(1));
	m * o1;
}
*/


int main()
{
	foo<float,double>(1,1,1); // failed
//      foo<float,double>();  works
	return 0;
}

you are trying to multiply a Matrix<double> and a Point<float> the overloaded binary * operator needs to be:

template<class T> class Matrix
{
  // ...
    template< typename U >
      Point<U> operator* (const Point<U>& p) const { return Point<U>(0,0,0); }
};

also,

template<class T, class C>
void foo(T x, T y, T z)
{
	Point<T> o1(0,0,0);

        // to the compiler this is the declaration of a function
        // with the signature Matrix<C> (*)(C,C,C)
	// Matrix<C> m(C(x),C(y), C(z)); 
        // modify to something like
        Matrix<C> m( C(x), C(y), ( C(z) ) ); 
        // note the extra () - ( C(z) ) is an expression
        // and not the declaration of a formal parameter 

	m * o1;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.