C++ 嵌套类前向声明​​错误

发布于 2024-07-08 23:29:10 字数 1063 浏览 14 评论 0原文

我试图在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月下凄凉 2024-07-15 23:29:10

表达式c->i取消对指向struct A::B的指针的引用,因此此时程序中的完整定义必须可见。

最简单的修复方法是使 A 的构造函数成为非内联的,并在定义 struct A::B 后为其提供主体。

The expression c->i dereferences the pointer to struct 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 of struct A::B.

人间☆小暴躁 2024-07-15 23:29:10

在定义结构体 B 之后定义 A 的构造函数。

Define the constructor for A AFTER the definition of struct B.

流绪微梦 2024-07-15 23:29:10

这是一个很好的例子,说明了为什么要将定义与声明分开。 您需要更改事物的顺序,以便在定义 struct A::B 定义之后定义构造函数 A::A()

class A
{
    struct B;
    B* c;
    A();
};

struct A::B
{
    int i;
};

A::A() { c->i; }

int main()
{
    return 0;
}

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 of struct A::B.

class A
{
    struct B;
    B* c;
    A();
};

struct A::B
{
    int i;
};

A::A() { c->i; }

int main()
{
    return 0;
}
囚你心 2024-07-15 23:29:10

有趣的是,我在 Stroustrup 书中提到的第 293 页(“11.12 A String Class”)中遇到了同样的问题。

印刷书中提供的示例似乎有问题,提供了以下内联方法,而不是在 struct Srep 的定义之后定义它们,

class String {
  // ...
  void check(int i) const { if (i<0 || rep->sz <=i) throw Range(); }
  char read(int i) const { return rep->s[i]; }
  void write(int i, char c) { rep=rep->get_own_copy(); rep->s[i]=c; }
  ...etc...

我用谷歌搜索了一下,找到了作者对此 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

class String {
  // ...
  void check(int i) const { if (i<0 || rep->sz <=i) throw Range(); }
  char read(int i) const { return rep->s[i]; }
  void write(int i, char c) { rep=rep->get_own_copy(); rep->s[i]=c; }
  ...etc...

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文