c++ 11和Visual Studio -无法解析的外部符号

C++11 and Visual Studio - unresolved external symbol

本文关键字:外部 符号 Visual Studio c++      更新时间:2023-10-16

我尝试在Visual Studio 2013中运行我的c++代码。该代码在gcc 4.9

之前运行。

我不知道为什么代码在Visual Studio中不运行。我把代码上传到GitHub上,这样每个人都可以看看它。请帮帮我。我真的不知道为什么代码不能在Visual Studio中运行。

Error   1   error LNK2019: unresolved external symbol "public: __thiscall ********::Property::Property<int,1>::Property<int,1>(class std::function<int __cdecl(void)> const &,class std::function<void __cdecl(int)> const &)" (??0?$Property@H$00@Property@********@@QAE@ABV?$function@$$A6AHXZ@std@@ABV?$function@$$A6AXH@Z@4@@Z) referenced in function _main    C:Users#######documentsvisual studio 2013Projects********PropertyPropertyTest.obj   Property
Error   2   error LNK2019: unresolved external symbol "public: __thiscall ********::Property::Property<int,1>::operator int const (void)" (??B?$Property@H$00@Property@********@@QAE?BHXZ) referenced in function _main C:Users#######documentsvisual studio 2013Projects********PropertyPropertyTest.obj   Property
Error   3   error LNK2019: unresolved external symbol "public: int __thiscall ********::Property::Property<int,1>::operator=(int const &)" (??4?$Property@H$00@Property@********@@QAEHABH@Z) referenced in function _main   C:Users#######documentsvisual studio 2013Projects********PropertyPropertyTest.obj   Property
Error   4   error LNK2019: unresolved external symbol "public: int __thiscall ********::Property::Property<int,1>::operator+=(int const &)" (??Y?$Property@H$00@Property@********@@QAEHABH@Z) referenced in function _main  C:Users#######documentsvisual studio 2013Projects********PropertyPropertyTest.obj   Property
Error   5   error LNK2019: unresolved external symbol "public: int __thiscall ********::Property::Property<int,1>::operator-=(int const &)" (??Z?$Property@H$00@Property@********@@QAEHABH@Z) referenced in function _main  C:Users#######documentsvisual studio 2013Projects********PropertyPropertyTest.obj   Property
Error   6   error LNK2019: unresolved external symbol "public: int __thiscall ********::Property::Property<int,1>::operator*=(int const &)" (??X?$Property@H$00@Property@********@@QAEHABH@Z) referenced in function _main  C:Users#######documentsvisual studio 2013Projects********PropertyPropertyTest.obj   Property
Error   7   error LNK2019: unresolved external symbol "public: int __thiscall ********::Property::Property<int,1>::operator/=(int const &)" (??_0?$Property@H$00@Property@********@@QAEHABH@Z) referenced in function _main C:Users#######documentsvisual studio 2013Projects********PropertyPropertyTest.obj   Property
Error   8   error LNK2019: unresolved external symbol "public: int __thiscall ********::Property::Property<int,1>::operator%=(int const &)" (??_1?$Property@H$00@Property@********@@QAEHABH@Z) referenced in function _main C:Users#######documentsvisual studio 2013Projects********PropertyPropertyTest.obj   Property
Error   9   error LNK2019: unresolved external symbol "public: __thiscall ********::Property::Property<int,2>::Property<int,2>(class std::function<int __cdecl(void)> const &)" (??0?$Property@H$01@Property@********@@QAE@ABV?$function@$$A6AHXZ@std@@@Z) referenced in function _main C:Users#######documentsvisual studio 2013Projects********PropertyPropertyTest.obj   Property
Error   10  error LNK2019: unresolved external symbol "public: __thiscall ********::Property::Property<int,2>::operator int const (void)" (??B?$Property@H$01@Property@********@@QAE?BHXZ) referenced in function _main C:Users#######documentsvisual studio 2013Projects********PropertyPropertyTest.obj   Property
Error   11  error LNK2019: unresolved external symbol "public: __thiscall ********::Property::Property<int,3>::Property<int,3>(class std::function<void __cdecl(int)> const &)" (??0?$Property@H$02@Property@********@@QAE@ABV?$function@$$A6AXH@Z@std@@@Z) referenced in function _main    C:Users#######documentsvisual studio 2013Projects********PropertyPropertyTest.obj   Property
Error   12  error LNK2019: unresolved external symbol "public: void __thiscall ********::Property::Property<int,3>::operator=(int)" (??4?$Property@H$02@Property@********@@QAEXH@Z) referenced in function _main    C:Users#######documentsvisual studio 2013Projects********PropertyPropertyTest.obj   Property
Error   13  error LNK1120: 12 unresolved externals  C:Users#######documentsvisual studio 2013Projects********DebugProperty.exe  Property

[关闭]

就像Praetorian写的那样,模板类不能轻易地分成头文件(.h)和源文件(.cpp)。原因是"编译器用给定的模板参数创建一个新类。"quote

将模板类拆分为头文件(.h)和源文件(.cpp)的解决方案如下:

// HEADER File test.h
#ifndef __TEST_FH__
#define __TEST_FH__
template<typename T>
class test {
public:
    T object;
    test();
    ~test();
};
#include "test.cpp"
#endif

// SOURCE File test.cpp
#ifdef __TEST_FH__
#ifndef __TEST_SOURCE_FH__
#define __TEST_SOURCE_FH__
template<typename T>
test::test() {
    //code
}
template<typename T>
test::~test() {
    //code
}
#endif
#endif

再次感谢你,禁卫军。