Hiding time and time

Source: Internet
Author: User

Hiding time and time

The following code is written by a student for the Project 3 time class in object of first cognition. But the error is a bit strange. He asked for help in the QQ group.

# 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); // This member function is set to private, is appropriate, please taste int hour; int minute; int sec ;}; void Time :: set_time () {char c1, c2; cout <"Enter the time (Format: hh: mm: ss)"; while (1) {cin> hour> c1> minute> c2> sec; if (c1! = ':' | C2! = ':') The cout <"format is incorrect. Enter" <endl; else if (! Is_time (hour, minute, sec) cout <"invalid Time. Enter" <endl; else break ;}} void Time: show_time () {cout Readers, can you compile it?

When I compile in CodeBlocks, a large number of prompts are displayed:

||=== 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)) ===|

The first prompt is "extra qualification 'time: 'on member 'set _ time'", which means Time: redundant. It is not redundant to define a member function outside the class!
The second prompt is "'void Time: set_time () 'cannot be overloaded". Time: set_time () cannot be overloaded.
I probably know what's going on.
Use the resource code formatter to organize the program format. The indentation changes, and the demon immediately appears.


The following is a conversation between me and my students in the QQ group. If the readers do not find any errors, they may wish to try it while they try and find out the problems as prompted by me.

Instructor he, 18:54:18
In Code: Blocks, you can sort out the format and you will know how low the error is. Then let's look back at these error prompts, which can lead to a lot of valuable things.

Instructor he, 18:55:28
Other students can also learn a lot from XX's low-level errors. Please try.

Students 18:57:37
Large

Instructor he, 18:59:26
This kind of mistake is worth a lot in the course of learning! After such a mistake, you have a special fortune.

Students 19:00:37
I still don't understand much about it.

Instructor he, 19:01:13
Have you retyped it?

Students 19:01:25
Queued

Instructor he, 19:01:34
Can you confirm that void Time: set_time () is out of class?

Students 19:02:37
Well, define out-of-class Write Functions in the class

Instructor he, 19:03:06
If the class is not defined, this function is the header,

Instructor he, 19:03:19
Class to be indented.

Instructor he, 19:03:46
Your class declaration is defined in the class.

Instructor he, 19:04:22
This is repeated.

Instructor he, 19:07:50
Found? Do you want to give me another clue?

Students 19:08:34
Just give it.

Instructor he, 19:09:07
Where does the void add_a_sec () defined in the class end?

Students 19:10:09
Parentheses

Instructor he, 19:10:26
Are there any bricks at hand?

Students 19:10:54
No, it's not an error. I went to buy a tofu brick.

Students 19:11:45
Thank you, Mr. He.

Instructor he, 19:11:48
Don't worry about being a teacher. You don't need to take a picture. I'm afraid it hurts. The learning process is normal.

Instructor he, 19:12:40
You can typeset the errors in the future. If the indentation is not normal, the problem arises. This is also the value of specification.

Students 19:13:00
Grace learned





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.