为什么以下代码无法编译以及如何修复它?我得到的错误是:
使用未声明的标识符“Foo”
虽然Foo
在错误发生的地方(在friend
声明于Bar
).
foo.h:
#ifndef FOO_H
#define FOO_H
#include "bar.h" // needed for friend declaration in FooChild
class Foo {
public:
void Func(const Bar&) const;
};
class FooChild : public Foo {
friend void Bar::Func(FooChild*);
};
#endif
foo.cpp:
#include "foo.h"
void Foo::Func(const Bar& B) const {
// do stuff with B.X
}
bar.h:
#ifndef BAR_H
#define BAR_H
#include "foo.h"
class Bar {
public:
void Func(FooChild*) {}
private:
int X;
friend void Foo::Func(const Bar&) const; // "use of undeclared identifier 'Foo'"
};
#endif
Here http://goo.gl/GUu8cf是上述代码的可编译在线版本。
Put FooChild
在它自己的头文件中。
foo.h:
#ifndef FOO_H
#define FOO_H
class Bar;
class Foo {
public:
void Func(const Bar&) const;
};
#endif
foochild.h:
#ifndef FOOCHILD_H
#define FOOCHILD_H
#include "foo.h"
#include "bar.h"
class FooChild : public Foo {
friend void Bar::Func(FooChild*);
};
#endif
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)