g++ - c++ 条件编译报错。 ‘Mytime’ has not been declared。
怪我咯
怪我咯 2017-04-17 12:04:03
0
2
1513

在一个类定义的头文件中使用 #ifdef,#define #endif。之后会报错, XXXX has not been declared。为什么会这样?
使用条件编译:
mytime.h:

include<iostream>

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:

include<iostream>

include "mytime.h"

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

去掉条件编译。就正确了。求指点。

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(2)
巴扎黑

Is ifndef misspelled causing a compilation error

阿神
#ifndef MYTIME_H
#define MYTIME_H
...
#endif
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template