gpt4 book ai didi

C++ : Class to convert time from 12 to 24 hours not recognizing its own data fields

转载 作者:行者123 更新时间:2023-11-30 04:12:44 24 4
gpt4 key购买 nike

所以我写了一个类作为作业,以民用格式花费时间并将其转换为军用格式...其他一切正常,除了我的转换方法无法识别其自身类的数据字段并显示错误

这是我的类(class)。

#include <iostream>
#include <string>
using namespace std;
class Time
{
friend ostream & operator << (ostream &, const Time &);
friend istream & operator >> (istream &, Time &);
private:
int chour;
int cmin;
int csec;
string AMPM;
int mhour;
int mmin;
int msec;
public:
Time ();
Time (int, int, int, string);
void set_hour(int);
void set_min(int);
void set_sec(int);
void set_AMPM(string);
int get_hour();
int get_min();
int get_sec();
string get_AMPM();
void convert2military();
void operator ++ (int);
bool operator == (const Time&);
};
Time ::Time()
{
chour=0;
cmin=0;
csec=0;
AMPM=" ";
mhour=0;
mmin=0;
msec=0;

}
Time :: Time( int _hour, int _min, int _sec, string _AMPM)
{
chour=_hour;
cmin=_min;
csec=_sec;
AMPM=_AMPM;
}
void Time :: set_hour (int _hour)
{
if (_hour>=0 && _hour<12)
{chour=_hour;}
}
void Time :: set_min (int _min)

{
if (_min>=0 && _min<60)
{
cmin=_min;
}
}
void Time :: set_sec (int _sec)
{
if (_sec>=0 && _sec<60)
{
csec=_sec;
}
}
void Time :: set_AMPM (string _AMPM)
{
AMPM=_AMPM;
}
int Time :: get_hour ()
{
return chour;
}
int Time :: get_min ()
{
return cmin;
}
int Time :: get_sec ()
{
return csec;
}
string Time :: get_AMPM ()
{
return AMPM;
}
void Time :: operator ++ (int)
{
if ( csec>=0 && csec<60)
{
csec++;
}
else
{
csec=0;
if (cmin>=0 && cmin<60)
{
cmin++;
}
else
{
cmin=0;
if (chour>=0 && chour<12)
{
chour++;
}
else
{
chour=0;
}
}
}
}
bool Time :: operator == (const Time & obj)
{
if (chour==obj.chour && cmin==obj.cmin && csec==obj.csec)
{
return true;
}
else
{
return false;
}
}
ostream & operator << (ostream & out, const Time & obj)
{
int choice;
out<<endl<<endl;
cout<<"Press 1 to view civilian time\nPress 2 to view military time :";
cin>>choice;
switch (choice)
{
case 1:
{
cout<<endl<<endl;
out<<"Hour : "<<obj.chour<<endl;
out<<"Minutes : "<<obj.cmin<<endl;
out<<"Seconds : "<<obj.csec<<endl;
out<<"Complete time : "<<obj.chour<<" : "<<obj.cmin<<" : "<<obj.csec<<" "<<obj.AMPM<<endl;
return out;
break;
}
case 2:
{
cout<<endl<<endl;
out<<"Hour : "<<obj.mhour<<endl;
out<<"Minutes : "<<obj.mmin<<endl;
out<<"Seconds : "<<obj.msec<<endl;
out<<"Complete time : "<<obj.mhour<<" : "<<obj.mmin<<" : "<<obj.msec<<endl;
return out;
break;
}
default:
{
cout<<"Invalid entry\n";
break;
}
}
}
istream & operator >> (istream & in, Time & obj1)
{
cout<<"Enter hours : ";
in>>obj1.chour;
cout<<"Enter minutes : ";
in>>obj1.cmin;
cout<<"Enter seconds : ";
in>>obj1.csec;
cout<<"AM or PM ? : ";
cin>>obj1.AMPM;
return in;
}
void Time :: convert2military ()
{
if (AMPM=="AM")
{
mhours=chours;
mmins=cmins;
msec=csec;
}
if (AMPM=="AM" && hours==12)
{
mhours=0;
}
if (AMPM=="PM")
{
mhours=12+chours;
mmins=cmins;
msec=csec;
}
if (AMPM=="PM" && hours==12)
{
mhours=chours;
}

}
void main ()
{
Time obj1(4,11,35,"PM");
obj1.convert2military();
cout<<obj1;
}

错误是这个函数

void Time :: convert2military ()
{
if (AMPM=="AM")
{
mhours=chours;
mmins=cmins;
msec=csec;
}
if (AMPM=="AM" && hours==12)
{
mhours=0;
}
if (AMPM=="PM")
{
mhours=12+chours;
mmins=cmins;
msec=csec;
}
if (AMPM=="PM" && hours==12)
{
mhours=chours;
}

}

这是我遇到的错误列表

1>------ Build started: Project: Time class, Configuration: Debug Win32 ------
1> time class.cpp
~ (178): error C2065: 'mhours' : undeclared identifier
~ (178): error C2065: 'chours' : undeclared identifier
~ (179): error C2065: 'mmins' : undeclared identifier
~ (179): error C2065: 'cmins' : undeclared identifier
~ (182): error C2065: 'hours' : undeclared identifier
~ (184): error C2065: 'mhours' : undeclared identifier
~ (188): error C2065: 'mhours' : undeclared identifier
~ (188): error C2065: 'chours' : undeclared identifier
~ (189): error C2065: 'mmins' : undeclared identifier
~ (189): error C2065: 'cmins' : undeclared identifier
~ (192): error C2065: 'hours' : undeclared identifier
~ (194): error C2065: 'mhours' : undeclared identifier
~ (194): error C2065: 'chours' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

最佳答案

复数错误(又名打字错误):您将字段称为 chourmhour,但您想使用 choursmhours

提示:如果编译器告诉您未声明标识符,那几乎总是拼写错误或缺少字段。

关于C++ : Class to convert time from 12 to 24 hours not recognizing its own data fields,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19725970/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com