藏身时间类中的妖孽
最后更新于:2022-04-01 14:38:21
下面的代码,是一位同学为《[初识对象](http://blog.csdn.net/sxhelijian/article/details/44116231)》中的[项目3时间类](http://blog.csdn.net/sxhelijian/article/details/44116309)写的。但错误有点诡异,他在QQ群中求助。
~~~
#include <iostream>
using namespace std;
class Time
{
public:
void set_time();
void show_time();
void add_sec(int);
void add_minute(int);
void add_hour(int);
void add_a_sec()
{
sec=sec+1;
if(sec==60)
{
sec=0;
minute=minute+1;
if(minute==60)
{
minute=0;
hour=hour+1;
}
}
void add_a_minute()
{
minute=minute+1;
if(minute==60)
{
minute=0;
hour=hour+1;
}
}
void add_an_hour()
{
hour=hour+1;
}
private:
bool is_time(int, int, int); //这个成员函数设置为私有的,是合适的,请品味
int hour;
int minute;
int sec;
};
void Time::set_time()
{
char c1,c2;
cout<<"请输入时间(格式hh:mm:ss)";
while(1)
{ cin>>hour>>c1>>minute>>c2>>sec;
if(c1!=':'||c2!=':')
cout<<"格式不正确,请重新输入"<<endl;
else if (!is_time(hour,minute,sec))
cout<<"时间非法,请重新输入"<<endl;
else
break;
}
}
void Time::show_time()
{
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
bool Time::is_time(int h,int m, int s)
{
if (h<0 ||h>24 || m<0 ||m>60 || s<0 ||s>60)
return false;
return true;
}
void Time::add_sec(int adds)
{
sec=sec+adds;
while(sec>=60)
{
sec=sec-60;
minute=minute+1;
if(minute==60)
{
minute=0;
hour=hour+1;
}
}
}
void Time::add_minute(int addm)
{
minute=minute+addm;
while(minute>=60)
{
minute=minute-60;
hour=hour+1;
}
}
void Time::add_hour(int addh)
{
hour=hour+addh;
}
int main( )
{
Time t1;
int adds,addm,addh;
cout<<"请输入要增加的秒,分,小时"<<endl;
cin>>adds>>addm>>addh;
t1.set_time( );
T1.show_time( );
t1.add_a_sec();
t1.add_a_minute();
t1.add_an_hour();
t1.add_sec(adds);
t1.add_minute(adds);
t1.add_hour(addh);
return 0;
}
~~~
读者朋友,你编译一下试试?
我在CodeBlocks中编译,提示一大堆:
~~~
||=== Build: Debug in example (compiler: GNU GCC Compiler) ===|
D:\CPP\codeBlock\example\main.cpp|43|error: extra qualification 'Time::' on member 'set_time' [-fpermissive]|
D:\CPP\codeBlock\example\main.cpp|43|error: 'void Time::set_time()' cannot be overloaded|
D:\CPP\codeBlock\example\main.cpp|6|error: with 'void Time::set_time()'|
D:\CPP\codeBlock\example\main.cpp|57|error: extra qualification 'Time::' on member 'show_time' [-fpermissive]|
D:\CPP\codeBlock\example\main.cpp|57|error: 'void Time::show_time()' cannot be overloaded|
D:\CPP\codeBlock\example\main.cpp|7|error: with 'void Time::show_time()'|
D:\CPP\codeBlock\example\main.cpp|61|error: extra qualification 'Time::' on member 'is_time' [-fpermissive]|
D:\CPP\codeBlock\example\main.cpp|67|error: extra qualification 'Time::' on member 'add_sec' [-fpermissive]|
D:\CPP\codeBlock\example\main.cpp|67|error: 'void Time::add_sec(int)' cannot be overloaded|
D:\CPP\codeBlock\example\main.cpp|8|error: with 'void Time::add_sec(int)'|
D:\CPP\codeBlock\example\main.cpp|81|error: extra qualification 'Time::' on member 'add_minute' [-fpermissive]|
D:\CPP\codeBlock\example\main.cpp|81|error: 'void Time::add_minute(int)' cannot be overloaded|
D:\CPP\codeBlock\example\main.cpp|9|error: with 'void Time::add_minute(int)'|
D:\CPP\codeBlock\example\main.cpp|90|error: extra qualification 'Time::' on member 'add_hour' [-fpermissive]|
D:\CPP\codeBlock\example\main.cpp|90|error: 'void Time::add_hour(int)' cannot be overloaded|
D:\CPP\codeBlock\example\main.cpp|10|error: with 'void Time::add_hour(int)'|
D:\CPP\codeBlock\example\main.cpp|109|error: expected '}' at end of input|
D:\CPP\codeBlock\example\main.cpp||In member function 'void Time::add_a_sec()':|
D:\CPP\codeBlock\example\main.cpp|13|error: 'sec' was not declared in this scope|
D:\CPP\codeBlock\example\main.cpp|17|error: 'minute' was not declared in this scope|
D:\CPP\codeBlock\example\main.cpp|21|error: 'hour' was not declared in this scope|
D:\CPP\codeBlock\example\main.cpp|25|error: a function-definition is not allowed here before '{' token|
D:\CPP\codeBlock\example\main.cpp|42|error: expected '}' at end of input|
D:\CPP\codeBlock\example\main.cpp||In member function 'void Time::set_time()':|
D:\CPP\codeBlock\example\main.cpp|48|error: 'hour' was not declared in this scope|
D:\CPP\codeBlock\example\main.cpp|48|error: 'minute' was not declared in this scope|
D:\CPP\codeBlock\example\main.cpp|48|error: 'sec' was not declared in this scope|
D:\CPP\codeBlock\example\main.cpp||In member function 'void Time::show_time()':|
D:\CPP\codeBlock\example\main.cpp|59|error: 'hour' was not declared in this scope|
D:\CPP\codeBlock\example\main.cpp|59|error: 'minute' was not declared in this scope|
D:\CPP\codeBlock\example\main.cpp|59|error: 'sec' was not declared in this scope|
D:\CPP\codeBlock\example\main.cpp||In member function 'void Time::add_sec(int)':|
D:\CPP\codeBlock\example\main.cpp|69|error: 'sec' was not declared in this scope|
D:\CPP\codeBlock\example\main.cpp|73|error: 'minute' was not declared in this scope|
D:\CPP\codeBlock\example\main.cpp|77|error: 'hour' was not declared in this scope|
D:\CPP\codeBlock\example\main.cpp||In member function 'void Time::add_minute(int)':|
D:\CPP\codeBlock\example\main.cpp|83|error: 'minute' was not declared in this scope|
D:\CPP\codeBlock\example\main.cpp|87|error: 'hour' was not declared in this scope|
D:\CPP\codeBlock\example\main.cpp||In member function 'void Time::add_hour(int)':|
D:\CPP\codeBlock\example\main.cpp|92|error: 'hour' was not declared in this scope|
D:\CPP\codeBlock\example\main.cpp||In member function 'int Time::main()':|
D:\CPP\codeBlock\example\main.cpp|101|error: 'T1' was not declared in this scope|
D:\CPP\codeBlock\example\main.cpp|103|error: 'class Time' has no member named 'add_a_minute'|
D:\CPP\codeBlock\example\main.cpp|104|error: 'class Time' has no member named 'add_an_hour'|
D:\CPP\codeBlock\example\main.cpp|109|error: expected unqualified-id at end of input|
||=== Build failed: 38 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
~~~
第一个提示“extra qualification 'Time::' on member 'set_time'”,说Time::多余,类外定义成员函数,不多余啊!
第二个提示“ 'void Time::set_time()' cannot be overloaded”,Time::set_time()不能被重载。
我大概知道怎么回事了。
用resource code formatter整理程序的格式,缩进发生变化,妖孽立刻现身。
下面是我和学生在QQ群中的对话,读者如果没有找出错误,不妨边看边试,按我的提示,自己找出问题来。
贺老师 2015-3-21 18:54:18
在Code::Blocks中,你整理一下格式,你会知道这个错有多么低级。然后回头再看这些错误提示,可以引出不少有价值的东西来。
贺老师 2015-3-21 18:55:28
其他同学,也可以从XX的低级错误中学到不少,试试吧。
学生 2015-3-21 18:57:37
糗大了
贺老师 2015-3-21 18:59:26
这样的错误,在学习过程中,价值连城!你经过了这样的错误,你就拥有了特别的财富。
学生 2015-3-21 19:00:37
还是有一点不大明白
贺老师 2015-3-21 19:01:13
重排版了吗?
学生 2015-3-21 19:01:25
排了
贺老师 2015-3-21 19:01:34
你能确认void Time::set_time()是在类外了吗?
学生 2015-3-21 19:02:37
嗯 类内定义 类外写函数
贺老师 2015-3-21 19:03:06
如果类外定义,这个函数该顶头才是,
贺老师 2015-3-21 19:03:19
类内定义,才会缩进去。
贺老师 2015-3-21 19:03:46
你类内声明,类内定义。
贺老师 2015-3-21 19:04:22
这就重复了。
贺老师 2015-3-21 19:07:50
发现了吗?要不要再给点线索?
学生 2015-3-21 19:08:34
在给点吧
贺老师 2015-3-21 19:09:07
你看类内定义的void add_a_sec(),在哪儿结束的?
学生 2015-3-21 19:10:09
落了一个括号
贺老师 2015-3-21 19:10:26
手边有砖吗?
学生 2015-3-21 19:10:54
不是 是括号括错了 我去买块豆腐砖
学生 2015-3-21 19:11:45
谢谢贺老
贺老师 2015-3-21 19:11:48
没砖为师就放心了。不必拍,我怕豆腐疼。学习过程中,很正常。
贺老师 2015-3-21 19:12:40
以后错误莫名其妙时,可以排版一下。若缩进不正常,问题一下就出来了。这也是讲究规范的价值。
学生 2015-3-21 19:13:00
恩恩 学到了