显然,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/