[英]c++ “Incomplete type not allowed” error accessing class reference information (Circular dependency with forward declaration)
[英]C++ Simple circular reference and forward declaration issue
我收到此错误:
错误 C3646:“bar”:未知的覆盖说明符
在 Visual Studio 2015 中尝试编译这个非常简单的 C++ 代码时:
主.cpp :
#include "Foo.h"
int main ()
{
return 0;
}
富.h :
#pragma once
#include "Bar.h"
class Foo
{
public:
Foo();
Bar bar;
};
酒吧.h :
#pragma once
#include "Foo.h"
class Bar
{
public:
Bar();
};
我得到了一个循环引用,因为每个 .h 都包含另一个,并且解决方案应该使用前向声明,但它们似乎不起作用,有人可以解释为什么吗? 我在这里发现了类似的问题,解决方案总是相同的,我想我错过了一些东西:)
循环引用完全是您自己制作的,您可以通过从 Bar.h 中删除#include "Foo.h"
来安全地删除它:
#pragma once
//#include "Foo.h" <---- not necessary, Bar does not depend on Foo
class Bar
{
public:
Bar();
};
您不需要在Foo.h
声明Bar
。 更一般的情况是,如果Foo
和Bar
相互依赖,则需要前向声明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.