+ 1
I have get the problem when compiling my code - undefined reference to `Rectangle<int>::Rectangle()'
**header file ** #ifndef WIKI_H #define WIKI_H template<class T> class Rectangle { private: T length; T breadth; void printarea(); public: Rectangle(); }; #endif **source file** #include "Wiki.h" #include <iostream> using namespace std; template<class T> void Rectangle<T>::printarea() { cout<< "Area = " << length*breadth <<endl; } template <class T> Rectangle<T>::Rectangle() { cout<< "Enter Length" <<endl; cin>> length; cout<< "Enter Breadth" <<endl; cin>> breadth; printarea(); } **main file** #include <iostream> #include "Wiki.h" using namespace std; int main() { Rectangle<int>X; return 0; }
1 Odpowiedź
0
Template classes have to be defined inline.
Which basically means copy pasta your definitions from the .cpp file to the .h ( Below the class ) and then add std:: where necessary since using namespace std; in a .h is to be avoided. ( Can still use it in local scope though )
Also you may want to use a space at Rectangle<int> <space> X.