2012-03-13 65 views
0

我有以下的模板類,錯誤在一個模板類的拷貝構造函數使用默認值

template <typename Real> 
class Marker { 

typedef Wm5::Vector3<Real> Position ; 
typedef Wm5::Vector3<Real> Normal ; 
typedef Wm5::Vector3<Real> Color ; 

public: 

    Marker(int id = -1, Position position = Wm5::Vector3<Real>::ZERO, Normal normal = Wm5::Vector3<Real>::ZERO, Color color = Wm5::Vector3<Real>::ZERO) 
: id_(id), position_(position), normal_(normal), color_(color), cluster_(-1) {} 

    ~Marker() {} 

private: 

    int id_ ; 
    Position position_ ; 
    Normal normal_ ; 
    Color color_ ; 
    int cluster_ ; 

}; 


template <typename T> 
class MarkerSet { 

    typedef Marker<T> MarkerT ;  
    typedef std::vector<MarkerT> MarkerVector ; 

public: 

    MarkerSet::MarkerSet(int id = -1, MarkerVector markers = MarkerVector()) 
    {id_ = id; markers_ = markers;}  

    MarkerSet::~MarkerSet() {} 

private: 

    int id_ ; 
    MarkerVector markers_ ; 

} ; 

當我試圖通過

MarkerSet<double> markerSet ; 

創建MarkerSet對象獲得這個連接錯誤,

error LNK2001: unresolved external symbol "public: __thiscall  MarkerSet<double>::MarkerSet<double>(int,class std::vector<class Marker<double>,class std::allocator<class Marker<double> > >)" ([email protected]@@[email protected][email protected][email protected]@@[email protected][email protected]@@@[email protected]@@[email protected]@@Z) 

我會非常感激,如果有人能給我一個指向正確的方向與regar指針ds到我做錯了什麼。

編輯:

好吧我已經縮小到一些奇怪的東西。

中.H

MarkerSet(int id = -1, MarkerVector markers = MarkerVector()) 
{id_ = id; markers_ = markers;}  

在的.cpp構建精細

而非.H

MarkerSet(int id = -1, MarkerVector markers = MarkerVector()) ; 

template <typename T> 
MarkerSet<T>::MarkerSet(int id, MarkerVector markers) { 

    id_ = id ; 
    markers_ = markers ; 

} 

錯誤列於上述的方式。

有什麼想法?

+0

是否有可能你有一個陳舊的對象文件,使用舊的'MarkerSet'定義?試試你的IDE的「乾淨」或「重建所有」選項? – aschepler 2012-03-13 18:02:50

+0

是的,我已經嘗試了一次清理並重建所有。 – oracle3001 2012-03-13 18:18:45

回答

1

你可以嘗試使用不同的編譯器嗎?我嘗試過使用它,下面的代碼適用於我的gcc。我取消了Wm5成員,因爲我沒有這些成員。粘貼頭和CPP:

test.h

#include <vector> 

template <typename Real> 
class Marker { 

public: 

    Marker(int id = -1,int _position=1) 
    : id_(id), position(_position){} 

    ~Marker() {} 

private: 

    int id_ ; 
    int position ; 
    Real r; 

}; 


template <typename T> 
class MarkerSet { 

    typedef Marker<T> MarkerT ;  
    typedef std::vector<MarkerT> MarkerVector ; 

public: 

    MarkerSet(int id = -1, MarkerVector markers = MarkerVector()) 
     {id_ = id; markers_ = markers;std::cout<<"Called"<<std::endl;}  

    ~MarkerSet() {} 

private: 

    int id_ ; 
    MarkerVector markers_ ; 

} ; 

TEST.CPP

#include <iostream> 
#include <vector> 
#include "test.h" 

using namespace std; 


int main(int argc, const char **argv) { 
    cout<<"Hello"<<endl; 
    MarkerSet<double> ms; 
    return -1; 

} 

CMD:

bash$ ./test 
Hello 
Called 
bash$ 

模板類定義需要在標題:

Why do C++ template definitions need to be in the header?

+0

好的,我已經準備好在我的VS2010機器上構建....我不知道這個\t MarkerSet(int id = -1,MarkerVector markers = MarkerVector()){id_ = id; marker_ = markers; std :: cout <<「Called」<< std :: endl;}和MarkerSet(int id = -1,MarkerVector markers = MarkerVector()); in .h and template MarkerSet :: MarkerSet(int id,MarkerVector markers){id_ = id; markers_ = markers;}在.cpp文件中 – oracle3001 2012-03-13 20:24:41

+0

我不包括MarkerSet ::之前的函數名稱。由於定義在類的內部,因此不需要按類名稱進行限定。海灣合作委員會實際上抱怨。所有的定義都在頭文件中,因爲這些都是模板類。 – Sid 2012-03-13 20:27:05

+0

我明白了。請參閱上面的編輯,以更清楚地解釋仍存在的問題。 – oracle3001 2012-03-13 20:32:46

0

可能是因爲你沒有#include實際構造體,因此鏈接器不會生成MarkerSet<double>::MarkerSet<double>(int,class std::vector<class Marker<double>,class std::allocator<class Marker<double> > >)"所需的代碼?

+0

只是試圖改變超出限制的可能性,仍然得到相同的錯誤。使用VS2010作爲編譯器。 – oracle3001 2012-03-13 18:00:37

+0

我不太清楚你的意思是「不要#include實際的構造函數體」。 – oracle3001 2012-03-13 18:16:53

+0

SOrry,我的意思正是Sid建議的,無論是將構造函數主體移動到頭文件中,還是將#include cpp-file與主體一起使用,不僅包括帶原型的頭文件。我的不好,我應該澄清它。 – gluk47 2012-03-14 21:20:10