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

在一个类定义的头文件中使用 #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

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

怪我咯
怪我咯

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

répondre à tous(2)
巴扎黑

ifndef是不是拼错导致编译错误

阿神
#ifndef MYTIME_H
#define MYTIME_H
...
#endif
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!