C++ 嵌套类前向声明错误
我试图在 A 类中声明并使用 B 类 并在 A 之外定义 B。
我知道这是可能的,因为 Bjarne Stroustrup
在他的书《C++ 编程语言》中使用了这个
(第 293 页,例如 String 和 Srep 类)。
所以这是我导致问题的最小代码段
class A{
struct B; // forward declaration
B* c;
A() { c->i; }
};
struct A::B {
/*
* we define struct B like this becuase it
* was first declared in the namespace A
*/
int i;
};
int main() {
}
这段代码在 g++ 中给出了以下编译错误:
tst.cpp: In constructor ‘A::A()’:
tst.cpp:5: error: invalid use of undefined type ‘struct A::B’
tst.cpp:3: error: forward declaration of ‘struct A::B’
我试图查看 C++ 常见问题解答,我得到的 closeset 是 此处 和 此处但是
这些不适用于我的情况。
我还从这里阅读此内容,但这并不能解决我的问题。
gcc 和 MSVC 2005 都会给出编译器错误
I am trying to declare and use a class B inside of a class A
and define B outside A.
I know for a fact that this is possible because Bjarne Stroustrup
uses this in his book "The C++ programming language"
(page 293,for example the String and Srep classes).
So this is my minimal piece of code that causes problems
class A{
struct B; // forward declaration
B* c;
A() { c->i; }
};
struct A::B {
/*
* we define struct B like this becuase it
* was first declared in the namespace A
*/
int i;
};
int main() {
}
This code gives the following compilation errors in g++ :
tst.cpp: In constructor ‘A::A()’:
tst.cpp:5: error: invalid use of undefined type ‘struct A::B’
tst.cpp:3: error: forward declaration of ‘struct A::B’
I tried to look at the C++ Faq and the closeset I got was here and here but
those don't apply to my situation.
I also read this from here but it's not solving my problem.
Both gcc and MSVC 2005 give compiler errors on this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
表达式
c->i
取消对指向struct A::B
的指针的引用,因此此时程序中的完整定义必须可见。最简单的修复方法是使
A
的构造函数成为非内联的,并在定义struct A::B
后为其提供主体。The expression
c->i
dereferences the pointer tostruct A::B
so a full definition must be visible at this point in the program.The simplest fix is to make the constructor of
A
non-inline and provide a body for it after the defintion ofstruct A::B
.在定义结构体 B 之后定义 A 的构造函数。
Define the constructor for A AFTER the definition of struct B.
这是一个很好的例子,说明了为什么要将定义与声明分开。 您需要更改事物的顺序,以便在定义
struct A::B
定义之后定义构造函数A::A()
。This is a good example of why you want to keep definitions separate from declarations. You need to change the order of things so that the constructor
A::A()
is defined after the definition ofstruct A::B
.有趣的是,我在 Stroustrup 书中提到的第 293 页(“11.12 A String Class”)中遇到了同样的问题。
印刷书中提供的示例似乎有问题,提供了以下内联方法,而不是在 struct Srep 的定义之后定义它们,
我用谷歌搜索了一下,找到了作者对此 String 类的最新实现,可在此处找到:
http://www2.research.att.com/~bs/string_example.c
他似乎对其进行了修改,使这些方法不再内联,以避免该线程中提到的问题。
Interestingly, I've bumped into the same problem with the page 293 ('11.12 A String Class') mentioned in the Stroustrup book.
The example provided in the printed book seems to be at fault, providing the following methods as inline, instead of defining them after the definition of struct Srep
I googled a bit, and found the author's latest implementation of this String Class, available here:
http://www2.research.att.com/~bs/string_example.c
He seems to have modified it so that these methods are no longer inline, to avoid the problem mentioned in this thread.