C ++类,其基类和循环包括 [英] C++ class, its base class and circular include includes

查看:159
本文介绍了C ++类,其基类和循环包括的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FILE#1(foo.h):

  #ifndef FOO_H_ 
#define FOO_H_
#includebaseclass.h
#includebar.h
class Bar;
class Foo:public baseclass {
public:
bar * varBar;
};
#endif

FILE#2(bar.h):

  #ifndef BAR_H_ 
#define BAR_H_
#includefoo.h
class Foo;
class Bar {
public:
Foo * varFoo;
};
#endif

FILE#3(baseclass.h):

  #ifndef BASECLASS_H_ 
#define BASECLASS_H_
#includefoo.h
class Foo;
class baseclass {
public:
list< Foo *> L;
};
#endif

但我在<$ c行的文件#1中遇到编译错误$ c> class Foo:public baseclass :

 错误: token 

如果我添加 class baseclass; bevor类声明,我得到这个错误:

 错误:无效使用不完整的类型»struct baseclass«


>询问你是否得到一个点。我已经尝试改变包括头的顺序,但没有运气到目前为止。
感谢任何提示。



编辑:注意:我使用包含守卫
EDIT2:它不限于指针, , 以防万一。
EDIT3:添加基础类(forgot Oo)
EDIT4:现在应该清楚,没有任何缺陷,问题依然存在于此代码中。

解决方案

你似乎发布的是在 Foo Bar >和 Bar 中的 Foo 成员。这是一个循环依赖,你需要打破 - 如果每个 Foo 包含 Bar ,其中包含 Foo 然后构造永不终止。

  class Foo:public baseclass {
public:
Bar varBar;
};

class Bar {
public:
Foo varFoo;
};相反,你需要使用一个指针或引用 Foo

Bar 中至少一个:

 类Bar; 
class Foo:public baseclass {
public:
Bar& varBar;
};

class Bar {
public:
Foo varFoo;
};

由于循环性破坏,而且只使用对象的引用,需要具有引用类型的完整定义,并且可以使用向前声明。



包含警卫对用户有好处,但尝试不依赖它们发展。如果编译器必须检查是否包含某些东西,它仍然在做工作,即使它对guard / pragmas有优化。你需要有一些了解什么取决于什么打破了初始的周期,并把文件保护不会帮助你。


FILE #1 (foo.h):

#ifndef FOO_H_
#define FOO_H_
#include "baseclass.h"
#include "bar.h"
class Bar;
class Foo : public baseclass {
public:
bar *varBar;
};
#endif

FILE #2 (bar.h):

#ifndef BAR_H_
#define BAR_H_
#include "foo.h"
class Foo;
class Bar {
public:
Foo *varFoo;
};
#endif

FILE #3 (baseclass.h):

#ifndef BASECLASS_H_
#define BASECLASS_H_
#include "foo.h"
class Foo;
class baseclass {
public:
list<Foo*> L;
};
#endif

But I get an compile error in file #1 in line class Foo : public baseclass:

Error: expected class-name before »{« token

If I add class baseclass; bevor class declaration, I get this error:

Error: invalid use of incomplete type »struct baseclass«

So my question is, how can I resolve circular dependencies with baseclasses?

Ask if you don't get somepoint. I allready tried to change the order of includeing the headers, but no luck so far. Thanks for any hint.

EDIT: Note: I am using include guards EDIT2: It is not limited to pointers, so I remove them, just in case. EDIT3: Added baseclass (forgot O.o) EDIT4: Now it should be clear and without anymore flaws, the problem persisits with this code.

解决方案

What you seem to have posted is to have a Bar member in the Foo, and a Foo member in the Bar. That is a circular dependency you need to break - if every Foo contains a Bar which contains a Foo then constructing either never terminates.

class Foo : public baseclass {
    public:
        Bar varBar;
};

class Bar {
    public:
        Foo varFoo;
};

Instead you need to use a pointer or reference to the Foo or Bar in at least one of them:

class Bar;
class Foo : public baseclass {
    public:
        Bar& varBar;
};

class Bar {
    public:
        Foo varFoo;
};

As the circularity is broken and you're only using a reference to the object, you don't need to have the full definition of the referred-to type, and can use a forward declaration.

Include guards are good for users, but try and not rely on them when developing. If the compiler has to check whether or not something has been included, it's still doing work even if it has optimisations for guards/pragmas. You do need to have some understanding of what depends on what to break the initial cycle, and putting guards on the files won't help you with that.

这篇关于C ++类,其基类和循环包括的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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