Reputation: 87
so i wrote a class as an assignment to take time in civilian format and convert it to military style... everything else is working fine except my convert method is not recogonizing the data field of its own class and displaying errors
Here is my 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;
}
Error is this function
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;
}
}
and here is a list of errors i'm getting
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 ==========
Upvotes: 1
Views: 4237
Reputation: 105886
Pluralizing errors (aka typo): You call your fields chour
or mhour
, but you want to use chours
or mhours
.
Hint: If the compiler tells you that an identifier is undeclared, it's almost always either a typo or a missing field.
Upvotes: 2
Reputation: 409176
Check the variable names used in the function, and compare them to the ones declared in the class definition.
E.g. mhours
versus mhour
.
Upvotes: 0