![](/img/trans.png)
[英]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.