我一直在VC ++ 2015平台上编码,并且在非重写的基类函数上总是得到未解决的外部符号错误。这是一个例子:
//BaseClass.h
template<class T>
class BaseClass {
public:
T* setRootPath(std::string&& rootPath);
....
}
//Implementations are in BaseClass.cpp
//ChildClass.h
class ChildClass:public BaseClass<ChildClass> {
public:
ChildClass* setWidth(const int width);
//No redefinition of "setRootPath" and hope not necessary
....
}
//Implementations are in ChildClass.cpp and i included BaseClass.cpp in this file as well
当我编译此代码时,它不会抛出任何错误。另一方面,在链接时,它表示无法通过适当的模板找到setRootPath所需的符号。为什么它找不到&#34; ChildClass* setRootPath(std::string&& rootPath)
&#34;功能?
我想问的是&#34;我应该在ChildClass中使用适当的返回类型声明该函数,而我想使用BaseClass中的相同定义&#34;?
MSVC抛出的错误是:
error LNK2001: unresolved external symbol "public: class ChildClass * __cdecl BaseClass<class ChildClass>::setRootPath(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &&)" (?setRootPath@?$BaseClass@VChildClass@3@$$QEAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
提前感谢您的帮助。