0

This is my code. It's to practice using templates.

Header

#ifndef H_rectangleType #define H_rectangleType #include <iostream> using namespace std; namespace Rectangle{ template <class myType> class rectangleType { //Overload the stream insertion and extraction operators friend ostream& operator << (ostream&, const rectangleType &); friend istream& operator >> (istream&, rectangleType &); public: void setDimension(myType l, myType w); myType getLength() const; myType getWidth() const; myType area() const; myType perimeter() const; void print() const; rectangleType<myType> operator+(const rectangleType<myType>&) const; //Overload the operator + rectangleType<myType> operator*(const rectangleType<myType>&) const; //Overload the operator * bool operator==(const myType&) const; //Overload the operator == bool operator!=(const myType&) const; //Overload the operator != rectangleType(); rectangleType(myType l, myType w); private: myType length; myType width; }; } #endif 

Member Definitions

#include <iostream> #include "rectangleType.h" using namespace std; namespace Rectangle{ template <class myType> void rectangleType<myType>::setDimension(myType l, myType w) { if (l >= 0) length = l; else length = 0; if (w >= 0) width = w; else width = 0; } template <class myType> myType rectangleType<myType>::getLength() const { return length; } template <class myType> myType rectangleType<myType>::getWidth()const { return width; } template <class myType> myType rectangleType<myType>::area() const { return length * width; } template <class myType> myType rectangleType<myType>::perimeter() const { return 2 * (length + width); } template <class myType> void rectangleType<myType>::print() const { cout << "Length = " << length << "; Width = " << width; } template <class myType> rectangleType<myType>::rectangleType(myType l, myType w) { setDimension(l, w); } template <class myType> rectangleType<myType>::rectangleType() { length = 0; width = 0; } template <class myType> rectangleType<myType> rectangleType<myType>::operator+ (const rectangleType<myType>& rectangle) const { rectangleType<myType> tempRect; tempRect.length = length + rectangle.length; tempRect.width = width + rectangle.width; return tempRect; } template <class myType> rectangleType<myType> rectangleType<myType>::operator* (const rectangleType<myType>& rectangle) const { rectangleType<myType> tempRect; tempRect.length = length * rectangle.length; tempRect.width = width * rectangle.width; return tempRect; } template <class myType> bool rectangleType<myType>::operator== (const myType& rectangle) const { return (length == rectangle.length && width == rectangle.width); } template <class myType> bool rectangleType<myType>::operator!= (const myType& rectangle) const { return (length != rectangle.length || width != rectangle.width); } template <class myType2> ostream& operator << (ostream& osObject, const rectangleType<myType2>& rectangle) { osObject << "Length = " << rectangle.length << "; Width = " << rectangle.width; return osObject; } template <class myType2> istream& operator >> (istream& isObject, rectangleType<myType2>& rectangle) { isObject >> rectangle.length >> rectangle.width; return isObject; } } 

Main

#include <iostream> //Line 1 #include "rectangleType.h" //Line 2 using namespace std; //Line 3 using namespace Rectangle; int main() //Line 4 { //Line 5 cout << "Enter the type of your Rectangle.\n1. int\n2. double\n3. float"; rectangleType<double> myRectangle(23, 45); //Line 6 int int1 = 1; int double1 = 2; int float1 = 3; int temp= 0; cin >> temp; //Line 7 if(temp == int1) { rectangleType<int> myRectangle(23, 45); rectangleType<int> yourRectangle; cout << "Line 8: myRectangle: " << myRectangle << endl; cout << "Line 9: Enter the length and width " <<"of a rectangle: "; cin >> yourRectangle; cout << endl; cout << "Line 12: yourRectangle: " << yourRectangle << endl; cout << "Line 13: myRectangle + yourRectangle: " << myRectangle + yourRectangle << endl; cout << "Line 14: myRectangle * yourRectangle: " << myRectangle * yourRectangle << endl; } if(temp == double1) { rectangleType<double> myRectangle(23, 45); rectangleType<double> yourRectangle; cout << "Line 8: myRectangle: " << myRectangle << endl; cout << "Line 9: Enter the length and width " <<"of a rectangle: "; cin >> yourRectangle; cout << endl; cout << "Line 12: yourRectangle: " << yourRectangle << endl; cout << "Line 13: myRectangle + yourRectangle: " << myRectangle + yourRectangle << endl; cout << "Line 14: myRectangle * yourRectangle: " << myRectangle * yourRectangle << endl; } if(temp == float1) { rectangleType<float> myRectangle(23, 45); rectangleType<float> yourRectangle; cout << "Line 8: myRectangle: " << myRectangle << endl; cout << "Line 9: Enter the length and width " <<"of a rectangle: "; cin >> yourRectangle; cout << endl; cout << "Line 12: yourRectangle: " << yourRectangle << endl; cout << "Line 13: myRectangle + yourRectangle: " << myRectangle + yourRectangle << endl; cout << "Line 14: myRectangle * yourRectangle: " << myRectangle * yourRectangle << endl; } return 0; 

}

Here,I'm getting these errors

1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "public: class Rectangle::rectangleType<float> __thiscall Rectangle::rectangleType<float>::operator*(class Rectangle::rectangleType<float> const &)const " (??D?$rectangleType@M@Rectangle@@QBE?AV01@ABV01@@Z) referenced in function _main 1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "public: class Rectangle::rectangleType<float> __thiscall Rectangle::rectangleType<float>::operator+(class Rectangle::rectangleType<float> const &)const " (??H?$rectangleType@M@Rectangle@@QBE?AV01@ABV01@@Z) referenced in function _main 1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "class std::basic_istream<char,struct std::char_traits<char> > & __cdecl Rectangle::operator>>(class std::basic_istream<char,struct std::char_traits<char> > &,class Rectangle::rectangleType<float> &)" (??5Rectangle@@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV12@AAV?$rectangleType@M@0@@Z) referenced in function _main 1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl Rectangle::operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Rectangle::rectangleType<float> const &)" (??6Rectangle@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV12@ABV?$rectangleType@M@0@@Z) referenced in function _main 1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "public: __thiscall Rectangle::rectangleType<float>::rectangleType<float>(void)" (??0?$rectangleType@M@Rectangle@@QAE@XZ) referenced in function _main 1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "public: __thiscall Rectangle::rectangleType<float>::rectangleType<float>(float,float)" (??0?$rectangleType@M@Rectangle@@QAE@MM@Z) referenced in function _main 1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "public: class Rectangle::rectangleType<double> __thiscall Rectangle::rectangleType<double>::operator*(class Rectangle::rectangleType<double> const &)const " (??D?$rectangleType@N@Rectangle@@QBE?AV01@ABV01@@Z) referenced in function _main 1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "public: class Rectangle::rectangleType<double> __thiscall Rectangle::rectangleType<double>::operator+(class Rectangle::rectangleType<double> const &)const " (??H?$rectangleType@N@Rectangle@@QBE?AV01@ABV01@@Z) referenced in function _main 1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "class std::basic_istream<char,struct std::char_traits<char> > & __cdecl Rectangle::operator>>(class std::basic_istream<char,struct std::char_traits<char> > &,class Rectangle::rectangleType<double> &)" (??5Rectangle@@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV12@AAV?$rectangleType@N@0@@Z) referenced in function _main 1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl Rectangle::operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Rectangle::rectangleType<double> const &)" (??6Rectangle@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV12@ABV?$rectangleType@N@0@@Z) referenced in function _main 1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "public: __thiscall Rectangle::rectangleType<double>::rectangleType<double>(void)" (??0?$rectangleType@N@Rectangle@@QAE@XZ) referenced in function _main 1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "public: class Rectangle::rectangleType<int> __thiscall Rectangle::rectangleType<int>::operator*(class Rectangle::rectangleType<int> const &)const " (??D?$rectangleType@H@Rectangle@@QBE?AV01@ABV01@@Z) referenced in function _main 1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "public: class Rectangle::rectangleType<int> __thiscall Rectangle::rectangleType<int>::operator+(class Rectangle::rectangleType<int> const &)const " (??H?$rectangleType@H@Rectangle@@QBE?AV01@ABV01@@Z) referenced in function _main 1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "class std::basic_istream<char,struct std::char_traits<char> > & __cdecl Rectangle::operator>>(class std::basic_istream<char,struct std::char_traits<char> > &,class Rectangle::rectangleType<int> &)" (??5Rectangle@@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV12@AAV?$rectangleType@H@0@@Z) referenced in function _main 1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl Rectangle::operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Rectangle::rectangleType<int> const &)" (??6Rectangle@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV12@ABV?$rectangleType@H@0@@Z) referenced in function _main 1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "public: __thiscall Rectangle::rectangleType<int>::rectangleType<int>(void)" (??0?$rectangleType@H@Rectangle@@QAE@XZ) referenced in function _main 1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "public: __thiscall Rectangle::rectangleType<int>::rectangleType<int>(int,int)" (??0?$rectangleType@H@Rectangle@@QAE@HH@Z) referenced in function _main 1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "public: __thiscall Rectangle::rectangleType<double>::rectangleType<double>(double,double)" (??0?$rectangleType@N@Rectangle@@QAE@NN@Z) referenced in function _main 1>C:\Users\jr\Documents\Visual Studio 2010\Projects\CMPE 126\Lab 2\Debug\Lab 2.exe : fatal error LNK1120: 18 unresolved externals 

I cant seem to make what these errors mean. Thanks inadvance

3
  • ... and once again, there is using namespace std; in the beginning of a header file. Commented Feb 11, 2013 at 11:16
  • @LihO Do you not need the "using namespace std;" my professor gave us the basic code and we had to edit it Commented Feb 12, 2013 at 0:23
  • It's considered a bad practice to have it in a header file. Have a look at this answer: stackoverflow.com/questions/1265039/using-std-namespace/… Commented Feb 12, 2013 at 8:02

2 Answers 2

2

You can't separate definition and implementation for templated classes. You have to put the functions in the header file as well.

Sign up to request clarification or add additional context in comments.

2 Comments

Actually you can separate them. You have to make sure they have been instantiated though.
@JoachimPileborg after putting the two codes together, i'm still getting a couple of error LNK2019 errors. I know it's a long code but is the formatting correct?
1

Your template function definitions should be visible by the calling code.

Inline them.

3 Comments

They don't have to be visible but they do have to be instantiated for all types using them.
@PeterWood +1 ... arguably overkill for this question though.
This whole question is overkill as many have asked it before (c:

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.