0

这个博客立方建议尝试四件事,然后或多或少地寻求帮助

  • 重建,检查
  • 检查运行时库,只有一个项目
  • 检查入口点,检查 SUBSYSTEM:CONSULE
  • 检查强制包含的 .lib 文件,我没有看到 #pragma comment(lib, ...)
  • 关于在链接器选项中打开 /VERBOSE 的东西,我没有看到该选项

我会发布一些代码,但这是一个 LNK 错误;它没有提供太多信息。

  • LNK2005:“类 std::vector,类 std::allocator >,类 std::allocator,类 std::allocator > > >,类 std::allocator,类 std::allocator >,类 std::allocator,类 std::allocator > > > > > list1" (?list1@@3V?$vector@V?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2 @@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@V?$allocator@ V?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@ std@@V?$allocator@D@2@@std@@@2@@std@@@2@@std@@A) 已在 msproject.obj 中定义
  • LNK2005:“类 std::vector,类 std::allocator >,类 std::allocator,类 std::allocator > > >,类 std::allocator,类 std::allocator >,类 std::allocator,类 std::allocator > > > > > list1" (?list1@@3V?$vector@V?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2 @@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@V?$allocator@ V?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@ std@@V?$allocator@D@2@@std@@@2@@std@@@2@@std@@A) 已在 msproject.obj 中定义

我看到它与 msproject 中已经定义的向量有关 - 我确保向量具有不同的名称。这是与标题有关的东西吗# include <vector>?我尝试注释掉一些#includes 以检查,但相同。

4

2 回答 2

2

似乎您list1不止一次定义。(顺便说一句,顾名思义它应该是 a std::list,但这超出了问题的范围)

你有没有

std::vector<std::string> list1;

在头文件中?该标题是否包含在多个翻译单元中?

如果你想要一个全局的,你需要extern在标题中使用:

extern std::vector<std::string> list1;

并将定义移动到单个实现文件中。

于 2012-07-25T21:01:07.137 回答
1

您已经多次定义了 list1。可能通过将它放在一个头文件中,然后您将其包含在多个 cpp 文件中。你应该这样做

// in header file
extern std:vector<whatever> list1;

// in one cpp file
std:vector<whatever> list1;

您的知识差距在于您不知道/不了解如何在具有多个源文件的程序中声明和定义全局变量。任何体面的 C++ 入门书籍都应该涵盖这一点。

于 2012-07-25T21:13:08.543 回答