2013-08-05 34 views
0

我想實現派生自模板的PointArray類。這裏是什麼PointArray我的HPP文件的樣子:LNK2019從模板派生時出錯

#ifndef POINTARRAY_H 
#define POINTARRAY_H 
#include <iostream> 
#include <sstream> 
#include <stdio.h> 
#include "Array.hpp" 

using namespace Abhishek::CAD; 
using namespace Abhishek::CONTAINERS; 

namespace Abhishek 
{ 
    namespace CONTAINERS 
    { 
     class PointArray : public Array<Point> 
     { 
     public: 
      PointArray();//Default constrcutor. 
      PointArray(int size);//Constructor with size argument. 
      PointArray(const PointArray& arr);//Copy constructor. 
      ~PointArray();//Destructor. 
      double Length() const;//Length function. 
     }; 

    } 
} 


#endif 

我CPP是這樣的:

#include <iostream> 
#include <sstream> 
#include <stdio.h> 
#include "PointArray.hpp" 

using namespace Abhishek::CAD; 
using namespace Abhishek::CONTAINERS; 

namespace Abhishek 
{ 
    namespace CONTAINERS 
    { 
     //Default constructor. 
     PointArray::PointArray(): Array<Point>() 
     { 
      cout<<"Point arr default cons"<<endl; 
     } 

     //Constructor with size argument. 
     PointArray::PointArray(int size) : Array<Point>(size) 
     { 

     } 

     //Copy constructor. 
     PointArray::PointArray(const PointArray& arr) : Array<Point>(arr) 
     { 

     } 

     //destrcutor. 
     PointArray::~PointArray() 
     { 

     } 
    } 
} 

我得到的LNK錯誤:

 error LNK2019: unresolved external symbol "public: __thiscall Abhishek::CONTAINERS::Array<class Abhishek::CAD::Point>::Array<class Abhishek::CAD::Point>(void)" ([email protected]@[email protected]@@@[email protected]@@[email protected]) referenced in function "public: __thiscall Abhishek::CONTAINERS::PointArray::PointArray(void)" ([email protected]@[email protected]@[email protected]) 
1>PointArray.obj : error LNK2019: unresolved external symbol "public: __thiscall Abhishek::CONTAINERS::Array<class Abhishek::CAD::Point>::Array<class Abhishek::CAD::Point>(int)" ([email protected]@[email protected]@@@[email protected]@@[email protected]@Z) referenced in function "public: __thiscall Abhishek::CONTAINERS::PointArray::PointArray(int)" ([email protected]@[email protected]@[email protected]@Z) 
1>PointArray.obj : error LNK2019: unresolved external symbol "public: __thiscall Abhishek::CONTAINERS::Array<class Abhishek::CAD::Point>::Array<class Abhishek::CAD::Point>(class Abhishek::CONTAINERS::Array<class Abhishek::CAD::Point> const &)" ([email protected]@[email protected]@@@[email protected]@@[email protected]@@Z) referenced in function "public: __thiscall Abhishek::CONTAINERS::PointArray::PointArray(class Abhishek::CONTAINERS::PointArray const &)" ([email protected]@[email protected]@[email protected]@@Z) 
1>PointArray.obj : error LNK2019: unresolved external symbol "public: __thiscall Abhishek::CONTAINERS::Array<class Abhishek::CAD::Point>::~Array<class Abhishek::CAD::Point>(void)" ([email protected]@[email protected]@@@[email protected]@@[email protected]) referenced in function [email protected]@[email protected]@[email protected]$0 
1>C:\Users\Rambo\Documents\Level 6\Section 4.2b\Exercise 3\Debug\Exercise 3.exe : fatal error LNK1120: 4 unresolved externals 

我不明白爲什麼這會正在發生。我包含了所有相關的頭文件和CPP文件。如果任何人都可以幫助,我會非常感激。

+0

定義類Point的地方在哪裏? – juanchopanza

+3

讓我猜測:'Array'模板的實現在「Array.cpp」中,而不是在「Array.hpp」中? – molbdnilo

+2

[什麼是未解決的外部問題,我該如何解決?答案:模板實現不可見](http://stackoverflow.com/a/12574417/673730)。 –

回答

0

您忘了發佈最相關的標題:聲明導致錯誤的類模板的標題。幾乎可以肯定,那標題聲明Array模板的默認構造函數:

Array(); 

但沒有定義它;要麼沒有定義,要麼你在源文件中有一個定義。

無論哪種情況,您都會遇到錯誤,因爲模板必須在使用它們的任何翻譯單元中定義。這意味着您需要在標題中定義構造函數(和任何其他成員函數),以將它們包含在使用的地方。

如果這不是問題,那麼請張貼標題,以便我們可以進一步調查。

+0

我其實只是想通了。我沒有在我的PointArray.hpp中包含Array.cpp文件,一旦錯誤消失了,我的構造函數被正確調用。感謝您的關注。 – Rambo223

+0

@ user2653401:您可能想重命名'Array.cpp'(或將它與'Array.hpp'合併),因爲它是一個頭文件,而不是源文件。使用誤導性的文件擴展名可能會混淆編譯器和程序員。 –