c++ - Getting an "error LNK2019: unresolved external symbol" -
this code. it's practice using templates.
header
#ifndef h_rectangletype #define h_rectangletype #include <iostream> using namespace std; namespace rectangle{ template <class mytype> class rectangletype { //overload stream insertion , 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 operator + rectangletype<mytype> operator*(const rectangletype<mytype>&) const; //overload operator * bool operator==(const mytype&) const; //overload operator == bool operator!=(const mytype&) const; //overload 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 { homecoming length; } template <class mytype> mytype rectangletype<mytype>::getwidth()const { homecoming width; } template <class mytype> mytype rectangletype<mytype>::area() const { homecoming length * width; } template <class mytype> mytype rectangletype<mytype>::perimeter() const { homecoming 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; homecoming 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; homecoming temprect; } template <class mytype> bool rectangletype<mytype>::operator== (const mytype& rectangle) const { homecoming (length == rectangle.length && width == rectangle.width); } template <class mytype> bool rectangletype<mytype>::operator!= (const mytype& rectangle) const { homecoming (length != rectangle.length || width != rectangle.width); } template <class mytype2> ostream& operator << (ostream& osobject, const rectangletype<mytype2>& rectangle) { osobject << "length = " << rectangle.length << "; width = " << rectangle.width; homecoming osobject; } template <class mytype2> istream& operator >> (istream& isobject, rectangletype<mytype2>& rectangle) { isobject >> rectangle.length >> rectangle.width; homecoming 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 type of 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: come in length , width " <<"of 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: come in length , width " <<"of 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: come in length , width " <<"of 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; } homecoming 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 create these errors mean. inadvance
you can't separate definition , implementation templated classes. have set functions in header file well.
c++
No comments:
Post a Comment