C ++错误: 'class'没有命名类型“和“invalid use of incomplete type'struct ...'” [英] C++ Errors: " 'class' does not name a type" and "invalid use of incomplete type ‘struct ...' "

查看:7296
本文介绍了C ++错误: 'class'没有命名类型“和“invalid use of incomplete type'struct ...'”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是一个非常重复的问题,也在这里在StackOverflow,但我不设法解决我的问题,甚至尝试不同的答案。所以,我有一些类:

Here is a very repetitive issue, also here in StackOverflow, but I do not manage to solve my problem even trying the different answers. So, I have some classes:

main.cpp:

#include "foo.h"
#include "bar.h"

...

foo.h:

#include "bar.h"
class foo {
  foo();
  bar& bind(bar &b);
  gru& bind(gru &g);
};

bar.h:

#include "foo.h"
class bar {
  bar();
  foo& bind(foo &f);
  gru& bind(gru &g);
};

显然,我有一个循环依赖。所以我得到臭名昭着的错误'bar'没有命名一个类型。在这种情况下,我添加类栏; foo 声明并删除 #include 。当我这样做,我得到:无效使用不完全类型'struct bar'

Clearly, I have a cyclic dependency. So I get the infamous error 'bar' does not name a type. In this case, I add class bar; to foo declaration and delete the #include. When I do that, I get: invalid use of incomplete type ‘struct bar'.

不同的方式,还添加 class foo; 到bar,但我总是有某种错误。在这后一种情况下,我得到:

I tried in some different ways, also adding class foo; to bar, but I always have some kind of error. In this last case I get:

bar.cpp:48:11: error: prototype for ‘foo& bar::bind(bar::foo&)’ does not match any in class ‘bar’
bar.h:55:12: error: candidates are: gru& bar::bind(gru&)
bar.h:54:13: error:                 bar::foo& bar::bind(bar::foo&)

此外,我从来没有抱怨 gru 。作为一个额外的信息,作为一个附加的信息,已经在这个程序,与 bar main 添加 foo

Plus, I never get any complain about gru. Which, as an additional information, was already there in this program, working perfectly along with bar and main, before I added foo.

任何有用的想法?非常感谢:)

Any helpful ideas? Thanks a lot :)

推荐答案

谢谢你们的答案。在很多方面,他们是有帮助的。

Thank you a lot guys for the answers. In many ways they were helpful.

最后,我意识到,我不得不重新排序所有 #include 在我的代码,因为,正如你可能已经意识到,有更多的编码,我在这里放在一个更简单的版本(对不起。)

In the end I realized that I had to reorder all the #include's in my code, because, as you may have realized, there were a lot more coding and what I put here was a simpler version (sorry for that).

,我的最终解决方案是在 foo.h 中包括 class bar; bar.h 中。然后重新排序 main.cpp 和Makefile中的include。

So, my final solution was to include class bar; in foo.h and class foo; in bar.h. Then reorder the includes in main.cpp and the Makefile.

再次感谢 - )

这篇关于C ++错误: 'class'没有命名类型“和“invalid use of incomplete type'struct ...'”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆