类头文件

时间:2011-12-12 22:19:02

标签: c++ windows visual-studio

的main.cpp

int main()
{
return 0;
}

cell.h

#pragma once

class _cell {};

cell.cpp

#include "cell.h"

experiment.h

#pragma once

class _experiment
{
_cell cell;
};

experiment.cpp

#include "experiment.h"
#include "cell.h"

错误:

experiment.h(5): error C2146: syntax error : missing ';' before identifier 'cell'
experiment.h(5): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
experiment.h(5): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

这让我疯了。有帮助吗?谢谢!

2 个答案:

答案 0 :(得分:1)

您必须#include <cell.h>来自experiment.h,否则在_cell中使用时experiment.h未定义。

答案 1 :(得分:1)

将包含从cell.cpp移动到.h

·H:

#pragma once
#include "cell.h"

class _experiment
{
_cell cell;
};

的.cpp:

#include "experiment.h"

类需要成员的定义来包含它们的实例。