新手入门,求大神指导c++指导友元函数重载运算符问题。
#include<iostream.h>classclock{inthour,minute,second;public:clock(intnh=0,intnm=0,int...
#include<iostream.h>
class clock
{
int hour,minute,second;
public:
clock(int nh=0,int nm=0,int ns=0)
{ hour=nh;minute=nm;second=ns;}
void display();
friend void operator ++(clock c1);
friend void operator ++(clock c1,int);
};
void clock::display()
{
cout<<hour<<"-"<<minute<<"-"<<second<<endl;
}
void operator ++(clock c1)
{
second++;
if(second>=60)
{
second=0;
minute++;
if(minute>=60)
{ minute=0;
hour++;
hour=hour%24;
}
}
cout<<"++clock:";
}
void operator ++(clock c1,int)
{
second++;
if(second>=60)
{
second=0;
minute++;
if(minute>=60)
{ minute=0;
hour++;
hour=hour%24;
}
}
cout<<"clock++:";
}
void main()
{
clock myclock(23,59,59);
myclock.display();
++myclock;
myclock.display();
myclock++;
myclock.display();
}
D:\c++\Cpp1.cpp(20) : error C2065: 'second' : undeclared identifier
D:\c++\Cpp1.cpp(24) : error C2065: 'minute' : undeclared identifier
D:\c++\Cpp1.cpp(27) : error C2065: 'hour' : undeclared identifier 展开
class clock
{
int hour,minute,second;
public:
clock(int nh=0,int nm=0,int ns=0)
{ hour=nh;minute=nm;second=ns;}
void display();
friend void operator ++(clock c1);
friend void operator ++(clock c1,int);
};
void clock::display()
{
cout<<hour<<"-"<<minute<<"-"<<second<<endl;
}
void operator ++(clock c1)
{
second++;
if(second>=60)
{
second=0;
minute++;
if(minute>=60)
{ minute=0;
hour++;
hour=hour%24;
}
}
cout<<"++clock:";
}
void operator ++(clock c1,int)
{
second++;
if(second>=60)
{
second=0;
minute++;
if(minute>=60)
{ minute=0;
hour++;
hour=hour%24;
}
}
cout<<"clock++:";
}
void main()
{
clock myclock(23,59,59);
myclock.display();
++myclock;
myclock.display();
myclock++;
myclock.display();
}
D:\c++\Cpp1.cpp(20) : error C2065: 'second' : undeclared identifier
D:\c++\Cpp1.cpp(24) : error C2065: 'minute' : undeclared identifier
D:\c++\Cpp1.cpp(27) : error C2065: 'hour' : undeclared identifier 展开
2个回答
展开全部
不知道是不是因为你的second, minute, hour 都是private. 试试这个:
class clock
{
public:
int hour,minute,second;
clock(int nh=0,int nm=0,int ns=0)
{ hour=nh;minute=nm;second=ns;}
void display();
friend void operator ++(clock c1);
friend void operator ++(clock c1,int);
};
class clock
{
public:
int hour,minute,second;
clock(int nh=0,int nm=0,int ns=0)
{ hour=nh;minute=nm;second=ns;}
void display();
friend void operator ++(clock c1);
friend void operator ++(clock c1,int);
};
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询