Maison  >  Questions et réponses  >  le corps du texte

g++ - c++ 条件编译报错。 ‘Mytime’ has not been declared。

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

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

怪我咯怪我咯2713 Il y a quelques jours1461

répondre à tous(2)je répondrai

  • 巴扎黑

    巴扎黑2017-04-17 12:06:03

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

    répondre
    0
  • 阿神

    阿神2017-04-17 12:06:03

    #ifndef MYTIME_H
    #define MYTIME_H
    ...
    #endif

    répondre
    0
  • Annulerrépondre