1

所以我写了一个课程作为一项任务,花时间以民用格式将其转换为军事风格......除了我的转换方法无法识别其自己的类的数据字段并显示错误之外,其他一切都工作正常

这是我的课。

#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 ==========
4

2 回答 2

2

复数错误(又名拼写错误):您将字段chour称为 or mhour,但您想使用choursor mhours

提示:如果编译器告诉你一个标识符是未声明的,它几乎总是一个错字或缺少的字段。

于 2013-11-01T11:50:08.903 回答
0

检查函数中使用的变量名,并将它们与类定义中声明的变量名进行比较。

例如mhoursmhour.

于 2013-11-01T11:50:07.640 回答