【发布时间】:2015-09-21 17:43:40
【问题描述】:
我正在阅读循环引用和前向声明。我确实理解在头文件中包含实现不是一个好的设计实践。但是我正在试验,无法理解这种行为。
使用以下代码(包含前向声明)我希望它能够构建,但是我收到了这个错误:
Error 1 error C2027: use of undefined type 'sample_ns::sample_class2'
头文件.hpp
#ifndef HEADER_HPP
#define HEADER_HPP
#include "Header2.hpp"
namespace sample_ns
{
class sample_class2;
class sample_class{
public:
int getNumber()
{
return sample_class2::getNumber2();
}
};
}
#endif
Header2.hpp
#ifndef HEADER2_HPP
#define HEADER2_HPP
#include "Header.hpp"
namespace sample_ns
{
class sample_class;
class sample_class2{
public:
static int getNumber2()
{
return 5;
}
};
}
#endif
显然我错过了一些东西。有人可以指出我为什么会收到此错误的正确方向。
【问题讨论】:
-
sampele_class=>sample_class你已经在这里发布了你的真实代码,你必须这样做。 -
正在试验代码并打错了。
-
将声明与定义分开。请看这个:stackoverflow.com/questions/625799/…
标签: c++ header-files forward-declaration circular-reference