【问题标题】:C++11 and Visual Studio - unresolved external symbol [duplicate]C ++ 11和Visual Studio - 未解析的外部符号[重复]
【发布时间】:2014-06-06 22:16:36
【问题描述】:

我尝试在 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\#######\documents\visual studio 2013\Projects\********\Property\PropertyTest.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\#######\documents\visual studio 2013\Projects\********\Property\PropertyTest.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\#######\documents\visual studio 2013\Projects\********\Property\PropertyTest.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\#######\documents\visual studio 2013\Projects\********\Property\PropertyTest.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\#######\documents\visual studio 2013\Projects\********\Property\PropertyTest.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\#######\documents\visual studio 2013\Projects\********\Property\PropertyTest.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\#######\documents\visual studio 2013\Projects\********\Property\PropertyTest.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\#######\documents\visual studio 2013\Projects\********\Property\PropertyTest.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\#######\documents\visual studio 2013\Projects\********\Property\PropertyTest.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\#######\documents\visual studio 2013\Projects\********\Property\PropertyTest.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\#######\documents\visual studio 2013\Projects\********\Property\PropertyTest.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\#######\documents\visual studio 2013\Projects\********\Property\PropertyTest.obj   Property
Error   13  error LNK1120: 12 unresolved externals  C:\Users\#######\documents\visual studio 2013\Projects\********\Debug\Property.exe  Property

[关闭]

【问题讨论】:

  • 首先谢谢你。现在它起作用了。不能将模板类拆分为 .h 和 .cpp 文件吗?我必须在头文件中实现漏洞代码吗?因为代码已经在 .h 和 .cpp 拆分的 gcc 4.9 上运行。这可能是 gcc 的特别之处吗?
  • 您可以将它们拆分,#include cpp 也可以。 gcc 在这方面并没有什么特别之处,你肯定在不知不觉中做了一些不同的事情。
  • @Praetorian 如何将您的评论设置为正确答案?还是因为它“只是”一条评论而不可能?
  • 您不能选择评论作为答案。不用担心,还有 2 票,这将作为重复关闭,所以请留下它。事实上,你自己也可以投票来加快进程:)
  • 这与 GitHub 无关。选择标签时请小心。

标签: c++ visual-studio c++11


【解决方案1】:

就像 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

再次非常感谢Praetorian。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-28
    相关资源
    最近更新 更多