Having trouble with c++ templates
-
I have a c++ class that contains the following: template class Poly { public: Poly(int order = 0) { this->order = order; coeffs = new double[order+1]; for( int i = 0;i<=order; i++ ) coeffs[i] = 0; } Poly( int order, T *coeffs); Poly( Poly &poly ); ~Poly() { delete []coeffs; } // other member functions are not shown. friend ostream &operator << ( ostream &out, Poly &poly ); private: // data members not show. }; This class is defined in a file called poly.h. In the file poly.cpp, I have the following function definition: template ostream & operator << ( ostream &out, Poly &poly ) { bool printedCoeff = false; . . . return out; } In a third file called test.cpp, I have: double coeffs[] = { 3, 1, 2, 1 }; Poly p1(3,coeffs), p2; cout << p1 << endl; I am using the Microsoft Visual Studio and I get a linker error because of the statement: cout << p1 << endl; I believe that my definition of the function overloading << is not properly getting instantiated. How do I fix this? Bob
-
I have a c++ class that contains the following: template class Poly { public: Poly(int order = 0) { this->order = order; coeffs = new double[order+1]; for( int i = 0;i<=order; i++ ) coeffs[i] = 0; } Poly( int order, T *coeffs); Poly( Poly &poly ); ~Poly() { delete []coeffs; } // other member functions are not shown. friend ostream &operator << ( ostream &out, Poly &poly ); private: // data members not show. }; This class is defined in a file called poly.h. In the file poly.cpp, I have the following function definition: template ostream & operator << ( ostream &out, Poly &poly ) { bool printedCoeff = false; . . . return out; } In a third file called test.cpp, I have: double coeffs[] = { 3, 1, 2, 1 }; Poly p1(3,coeffs), p2; cout << p1 << endl; I am using the Microsoft Visual Studio and I get a linker error because of the statement: cout << p1 << endl; I believe that my definition of the function overloading << is not properly getting instantiated. How do I fix this? Bob