在一个类定义的头文件中使用 #ifdef,#define #endif。之后会报错, XXXX has not been declared。为什么会这样?
使用条件编译:
mytime.h:
using namespace std;
#ifdef MYTIME_H
#define MYTIME_H
class Mytime{
private:
int hou;
int min;
public:
Mytime(int mm_hou = 1,int mm_min = 10){
hou = mm_hou;
min = mm_min;
};
~Mytime()
{
cout<<"destructor ~Mytime()"<<endl;
};
void show();
friend Mytime * operator +(Mytime &t,Mytime &t1);
};
#endif
mtiem.cpp:
using namespace std;
void Mytime::show()
{
cout<<hou<<"hours "<<min<<"minutes"<<endl;
};
Mytime * operator +(Mytime &t,Mytime &t1)
{
return new Mytime(t.hou+t1.hou,t.min+t1.min);
};
int main()
{
Mytime t;
t.show();
Mytime t2 = Mytime(10,20);
Mytime *t3 = t+t2;
t3->show();
}
报错:
‘mytime’ does not name a type
‘Mytime’ has not been declared
去掉条件编译。就正确了。求指点。