【问题标题】:Linking problems template with return将问题模板与返回链接
【发布时间】:2010-09-06 17:22:47
【问题描述】:

我在将方法的参数作为模板返回时遇到了一些问题,请看:

// CTestClass.h
template<class T> 
class CTestClass 
{
public:
    T getNewValue();
};

// CTestClass.cpp
template<class T> 
T CTestClass<T>::getNewValue()
{
    return 10; // just for tests I'm returning hard coded 10
}

// main.cpp
int _tmain(int argc, _TCHAR* argv[])
{
    CTestClass<int> s;
    int a = s.getNewValue();
    return 0;
}

我收到以下错误:

错误 LNK2019:函数 _wmain 中引用了无法解析的外部符号“public: int __thiscall CTestClass::getNewValue(void)”(?getNewValue@?$CTestClass@H@@QAEHXZ)

【问题讨论】:

    标签: c++ templates return-value


    【解决方案1】:

    您需要阅读 C++ 常见问题解答"Why can't I separate the definition of my templates class from its declaration and put it inside a .cpp file?"

    实际上,你需要在头文件中定义CTestClass&lt;T&gt;::getNewValue()。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-19
      • 1970-01-01
      • 2010-10-06
      • 2021-06-16
      • 2011-06-28
      • 1970-01-01
      相关资源
      最近更新 更多