C++11 标准的 7.1.3[9]
部分指出:
If the typedef declaration defines an unnamed class (or enum), the first typedef-name declared by the declaration to be that class type (or enum type) is used to denote the class type (or enum type) for linkage purposes only (3.5).
[示例:
typedef struct { } *ps, S; // S is the class name for linkage purposes
——结束示例]
这意味着在下面的示例中,Foo
应该用作未命名类的名称,并在下面的示例中用于链接目的:
//foo1.cpp
typedef class
{
public: int f();
} Foo;
int Foo::f()
{
return 5;
}
由于 Foo::f()
是在 foo1.cpp
中定义的,编译器不应该报错。
//foo2.cpp
typedef class
{
public: int f();
} Foo;
Foo foo;
int main()
{
return foo.f();
}
但是,GCC 4.8
出现链接错误。我错过了什么吗?
$g++ foo1.cpp foo2.cpp
/tmp/ccMwHawT.o: In function `main':
713Y51.cpp:(.text+0x24): undefined reference to `Foo::f()'
collect2: error: ld returned 1 exit status
最佳答案
我认为你是对的。除了您的问题中已经存在的内容之外,标准没有什么可以添加的。
这是一个 long-standing bug在海湾合作委员会。 clang 同意你的解释并接受你的程序。
关于c++ - typedef 声明定义未命名类时链接失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25898127/