【问题标题】:Linking a template class using another template class (error LNK2001)使用另一个模板类链接一个模板类(错误 LNK2001)
【发布时间】:2010-06-11 12:36:11
【问题描述】:

我使用一个抽象模板类和两个子类实现了“策略”设计模式。是这样的:

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)" (?alter@?$Neighbourhood@VLotSolutionInformation@@@lsc@@UAEXAAV?$vector@VLotSolutionInformation@@V?$allocator@VLotSolutionInformation@@@std@@@std@@HH@Z)

【问题讨论】:

    标签: c++ visual-studio visual-c++ templates abstract-class


    【解决方案1】:

    还有另一个子类,就像 这个,alter 是在 cpp 文件。

    不行-它必须在标题中。

    【讨论】:

    • 哦,是的,这也是。由于 C++ 非常适合做很多事情,我什至忘记了它作为一种 OO 语言是多么糟糕......:/
    【解决方案2】:

    这似乎是一个相当新手的错误。由于 Neighborhood 是一个抽象类,我必须始终将它用作指针(EDIT: 或引用),因为它绝不能被实例化。

    更改了签名,效果很好。

    编辑:另外,感谢 Neil,我知道“它必须在标题中”。

    【讨论】:

    • 事实上,您可能更喜欢使用对 Neighborhood 的引用而不是指针。
    • 如果不是说你必须使用指针,我会赞成。 @Luc 说什么
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    相关资源
    最近更新 更多