2013-02-11 167 views
0

這是我的代碼。這是練習使用模板。獲取「錯誤LNK2019:無法解析的外部符號」

頁眉

#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 

成員定義

#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; 
} 
} 

主要

#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;           

}

在這裏,我得到這些錯誤

1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "public: class         Rectangle::rectangleType<float> __thiscall Rectangle::rectangleType<float>::operator*(class Rectangle::rectangleType<float> const &)const " ([email protected]@[email protected]@[email protected]@@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 " ([email protected]@[email protected]@[email protected]@@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> &)" ([email protected]@[email protected][email protected]@[email protected]@@[email protected]@[email protected][email protected]@[email protected]@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 &)" ([email protected]@[email protected][email protected]@[email protected]@@[email protected]@[email protected][email protected]@[email protected]@Z) referenced in function _main 
1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "public: __thiscall Rectangle::rectangleType<float>::rectangleType<float>(void)" ([email protected]@[email protected]@[email protected]) referenced in function _main 
1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "public: __thiscall Rectangle::rectangleType<float>::rectangleType<float>(float,float)" ([email protected]@[email protected]@[email protected]@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 " ([email protected]@[email protected]@[email protected]@@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 " ([email protected]@[email protected]@[email protected]@@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> &)" ([email protected]@[email protected][email protected]@[email protected]@@[email protected]@[email protected][email protected]@[email protected]@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 &)" ([email protected]@[email protected][email protected]@[email protected]@@[email protected]@[email protected][email protected]@[email protected]@Z) referenced in function _main 
1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "public: __thiscall Rectangle::rectangleType<double>::rectangleType<double>(void)" ([email protected]@[email protected]@[email protected]) 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 " ([email protected]@[email protected]@[email protected]@@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 " ([email protected]@[email protected]@[email protected]@@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> &)" ([email protected]@[email protected][email protected]@[email protected]@@[email protected]@[email protected][email protected]@[email protected]@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 &)" ([email protected]@[email protected][email protected]@[email protected]@@[email protected]@[email protected][email protected]@[email protected]@Z) referenced in function _main 
1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "public: __thiscall Rectangle::rectangleType<int>::rectangleType<int>(void)" ([email protected]@[email protected]@[email protected]) referenced in function _main 
1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "public: __thiscall Rectangle::rectangleType<int>::rectangleType<int>(int,int)" ([email protected]@[email protected]@[email protected]@Z) referenced in function _main 
1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "public: __thiscall Rectangle::rectangleType<double>::rectangleType<double>(double,double)" ([email protected]@[email protected]@[email protected]@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 

我似乎不能做出這些錯誤的含義。非常感謝

+0

...再一次,在頭文件的開頭有'using namespace std;'。 – LihO 2013-02-11 11:16:52

+0

@LihO你不需要「使用命名空間標準;」我的教授給了我們基本的代碼,我們必須編輯它 – 2013-02-12 00:23:46

+0

將它放在頭文件中被認爲是不好的做法。看看這個答案:http://stackoverflow.com/questions/1265039/using-std-namespace/1265092#1265092 – LihO 2013-02-12 08:02:24

回答

2

您不能分隔模板類的定義和實現。你必須把這些函數放在頭文件中。

+0

其實你可以分開他們。你必須確保它們已經被實例化了。 – 2013-02-11 11:18:44

+0

@JoachimPileborg把這兩個代碼放在一起後,我仍然得到了一些錯誤LNK2019錯誤。我知道這是一個很長的代碼,但格式正確嗎? – 2013-02-12 00:26:06

1

您的模板功能定義應該可以被調用代碼看到。

將它們內聯。

+2

它們不必是可見的,但它們必須爲使用它們的所有類型實例化。 – 2013-02-11 11:19:53

+0

@PeterWood +1 ...雖然這個問題可以說是有點矯枉過正。 – 2013-02-11 12:11:28

+0

這個問題的整個過程很多人都問過(c: – 2013-02-11 12:12:30

相關問題