【发布时间】:2017-02-08 11:31:13
【问题描述】:
我收到此错误:
错误 C3646:“bar”:未知覆盖说明符
尝试在 Visual Studio 2015 中编译这个非常简单的 C++ 代码时:
main.cpp:
#include "Foo.h"
int main ()
{
return 0;
}
Foo.h:
#pragma once
#include "Bar.h"
class Foo
{
public:
Foo();
Bar bar;
};
Bar.h:
#pragma once
#include "Foo.h"
class Bar
{
public:
Bar();
};
我知道有一个循环引用,因为每个 .h 都包含另一个,并且解决方案应该使用 前向声明,但它们似乎不起作用,有人可以解释为什么吗?我在这里发现了类似的问题,并且解决方案总是相同的,我想我错过了一些东西:)
【问题讨论】:
-
那么......你的前向声明在哪里?
-
如果你想在
Foo之前使用Bar的前向声明,那么Bar bar;是一个错误,因为此时编译器应该知道Bar的大小,但是你可以使用聚合:Bar* bar.
标签: c++ class visual-studio-2015 include forward-declaration