我有很多课程,它们彼此非常接近,例如
class A
{
//code
};
class B
{
A* field;
//code
};
class C: public B
{
//code
};
等等。而且我想将它们放在单独的标题中(A.h
,B.h
...),但为了避免将每个标题添加到项目中,我需要一个类似的标题myLib.h
,这只是一个包含我编写的所有类的标题。我如何实现它?
我也认为不要使用#pragma once;
并使其正常工作
#ifndef _MY_LIB_H
#define _MY_LIB_H
#endif
我应该把它放在哪里?在每个标题中?
我试过这样做
class A;
class B;
...
在myLib.h
但是添加myLib.h
到main.cpp
并不足以在那里使用 A 或 B 对象。另外,在B.h
那
#inlude myLib.h
void someMethod()
{
//field is instance of A
this.field.otherMethod();
}
导致错误,因为 A 的方法是在 中声明的A.h
,而不是在 中myLib.h
。
对不起,冗长而纠结的问题。