【发布时间】:2014-01-28 08:20:48
【问题描述】:
这是一个非常重复的问题,也在 StackOverflow 中,但即使尝试不同的答案,我也无法解决我的问题。所以,我有一些课程:
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' does not name a type。在这种情况下,我将class bar; 添加到foo 声明并删除#include。当我这样做时,我得到:invalid use of incomplete type ‘struct bar'。
我尝试了一些不同的方法,也将class foo; 添加到 bar,但我总是遇到某种错误。在最后一种情况下,我得到:
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 的抱怨。作为附加信息,在我添加 foo 之前,该程序中已经存在,与 bar 和 main 完美配合。
有什么有用的想法吗?非常感谢:)
【问题讨论】:
-
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'.据我所知,这应该已经足够了。这是你真正的测试用例吗?您能在我们眼前编译和运行的实时代码粘贴网站上向我们展示吗? -
在您发布的代码中没有无效使用不完整类型。您在哪里以及如何以需要完整类型的方式使用
bar?
标签: c++ types compilation include cyclic-dependency