c++ - 内联非成员函数内的本地类使用 MSVC2005 生成 LNK2005

标签 c++ visual-c++-2005 inlining local-class lnk2005

显然,MSVC2005 未能内联本地类的成员函数,这导致了 LNK2005。

我在编译以下内容时遇到此 LNK2005 错误:

common.h内容:

inline void wait_what()
{
  struct wtf
  {
    void ffffuuu() {}

  } local;
}

foo.cpp内容:

#include "common.h"

void foo()
{
  wait_what();
}

bar.cpp内容:

#include "common.h"

void bar()
{
  wait_what();
}

LNK2005.cpp内容:

// forward declarations
void foo();
void bar();

int main()
{
  foo();
  bar();

  return 0;
}

错误信息是:

error LNK2005: "public void __thiscall `void__cdecl wait_what(void)'::`2'::wtf::ffffuuu(void)" (?ffffuuu@wtf?1??wait_what@@YAXXZ@QAEXXZ) already defined in bar.obj

关于本地类,ISO IEC 14882-2003 规定:

9.8 Local class declarations

A class can be defined within a function definition; such a class is called a local class. The name of a local class is local to its enclosing scope. The local class is in the scope of the enclosing scope, and has the same access to names outside the function as does the enclosing function. Declarations in a local class can use only type names, static variables, extern variables and functions, and enumerators from the enclosing scope.

An enclosing function has no special access to members of the local class; it obeys the usual access rules (clause 11). Member functions of a local class shall be defined within their class definition, if they are defined at all.

我错过了什么吗?

对我来说,这看起来像是一个编译器错误。 GCC 和 MSVC2008 编译它就可以了。但是,我想知道他们是否真的会内联调用,或者只是在链接阶段丢弃两个符号之一。有趣的是,您可以注意到甚至没有调用这个本地类成员函数。

我想知道 MSVC2005 是否有解决方法。我尝试在 MSDN 中搜索这个典型问题,但没有取得多大成功:我什至无法找到编译器的已知错误列表。


附件:LNK2005.zip

最佳答案

这是 Visual Studio 2005 中的一个错误,在 vs 2008 中已修复

关于c++ - 内联非成员函数内的本地类使用 MSVC2005 生成 LNK2005,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2078087/

相关文章:

c++ - 数组模板和内置数组类型之间有区别吗?

c++ - 数组名和另一个指向第一个元素的指针有什么区别?

c++ - 在使用 Visual Studio 2005 调试时调用函数?

c - 内联函数

C:将预编译代码编译为内联

c++ - 访问由 std::shared_ptr 包装的类的运算符重载

c++ - 在 C++ 中明确禁止堆分配

c++ - 我可能会在 Visual Studio 2005 中收到此 "Symbol not defined"错误的原因有哪些(包括屏幕截图)

c++ - OpenMP:有人知道堆损坏的原因吗?

c++ - 当我可以将对象的实际类型指定为模板参数时内联虚函数