2010-06-11 102 views
0

我使用抽象模板類和兩個子類實現了「策略」設計模式。是這樣的:使用其他模板類鏈接模板類(錯誤LNK2001)

template <class T> 
class Neighbourhood { 
public: 
    virtual void alter(std::vector<T>& array, int i1, int i2) = 0; 
}; 

template <class T> 
class Swap : public Neighbourhood<T> { 
public: 
    virtual void alter(std::vector<T>& array, int i1, int i2); 
}; 

還有一個子類,就像這個,alter在cpp文件來實現。好的!現在,我宣佈的另一種方法,在另一個類(當然也包括neighbourhood頭文件),像這樣:

void lSearch(/*parameters*/, Neighbourhood<LotSolutionInformation> nhood); 

它編譯罰款和乾淨。當開始聯繫起來,我得到以下錯誤:

1>SolverFV.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall lsc::Neighbourhood<class LotSolutionInformation>::alter(class std::vector<class LotSolutionInformation,class std::allocator<class LotSolutionInformation> > &,int,int)" ([email protected][email protected]@@@[email protected]@[email protected]@@[email protected]@@@[email protected]@@[email protected]@[email protected]) 

回答

0

這似乎是一個漂亮的菜鳥錯誤。因爲Neighbourhood是一個抽象類,所以我必須將它始終用作指針(EDIT或引用),因爲它絕不能被實例化。

更改簽名,它工作正常。

編輯:另外,感謝尼爾,我知道「它必須在標題中」。

+1

實際上,您可能更喜歡使用對鄰居而不是指針的引用。 – 2010-06-11 14:33:14

+0

我會upvote,如果它不是說你必須使用指針。 @Luc說什麼 – 2010-06-12 10:32:56

+0

謝謝你們倆:) – 2010-06-30 13:18:11

3

There's another subclass, just like this one, and alter is implemented in the cpp file.

沒有做不到的 - 它必須是在頭中。

+1

哦,是的,這也是。由於C++對於很多事情來說都很棒,所以我甚至忘記了它是如何被OO語言吸引的......:/ – 2010-06-11 13:18:31