C ++ classes and variables

I am relatively new to C ++ programming. I am learning how classes work, and I have a problem with the following code:

#include <iostream> using namespace std; class time { public: time(); void settime (int, int, int); void printuniversal (); void printstandard (); private: int hour; int minute; int second; }; time::time() { hour = minute = second = 0; } void time::settime (int h, int m, int s) { hour = (h >= 0 && h < 24) ? h : 0; minute = (m >= 0 && m < 60) ? m : 0; second = (s >= 0 && s < 60) ? s : 0; } void time::printuniversal() { cout << hour << ":" << minute << ":" << second << ":" << endl; } void time::printstandard() { cout << ((hour == 0 || hour == 12) ? 12 : hour % 12) << ":" << minute << ":" << second << (hour < 12 ? "AM" : "PM") << endl; } int main () { time t; cout << "Initial universal time: " << t.printuniversal(); cout << "\nInitial standard time: " << t.printstandard(); t.settime(13,27,6); cout << "\nNew universal time: " << t.printuniversal(); cout << "\nNew standard time: " << t.printstandard(); return 0; } 

The error I get is: classi.cpp: 42: 6: error: expected '; until classi.cpp: 43: 39: error: 't was not declared in this area

Is there something that I did not quite understand about classes? Why doesn't he recognize the variable "time"?

+6
source share
3 answers

This should teach you not to have nasty using directives, such as:

 using namespace std; 

And especially not in the area of โ€‹โ€‹the namespace (even worse if in the header files). There is a function std::time() in the standard library whose name collides with the name of your type.

This ambiguity can be resolved using the class keyword in the t declaration:

 class time t; 

However, a much better way would be to remove the using directive and define entity names from the standard namespace, thus writing (for example):

  std::cout << "Initial universal time: " // ^^^^^ 

Note that this may not be enough, since library implementations are allowed to put objects from the standard C library into the global namespace. In this case, removing the disgusting using directive would not help eliminate the ambiguity.

Therefore, I also suggest avoiding giving your own entities (types, functions, variables, ...) the same name as entities from the standard library, or putting them in your own namespace at least.

In addition, expressions such as:

 cout << "Initial universal time: " << t.printuniversal(); // ^^^^^^^^^^^^^^^^^^^^^ // printuniversal() returns void! 

Poorly formed since printuniversal() returns void . You should just do:

 cout << "Initial universal time: "; t.printuniversal(); 

The same applies to all similar expressions.

+14
source

You should not name your class time , or you should avoid using using namespace std . Instead, you can make statements like using std::cout , using std::endl , etc. I personally never use "use", I always leave std ::, it simplifies my search in the source code.

Anyway, I checked here , removing using namespace std really doesn't help (see discussion above ). Play in a safe place and change the name to a class. In any case, the above suggestions remain.

+3
source

An alternative to removing the "using namespace std" directive is to place your code in a namespace to avoid name collisions. This can be done as follows:

 namespace time_utils { class time { public: time(); void settime (int, int, int); void printuniversal (); void printstandard (); private: int hour; int minute; int second; }; }; time_utils::time::time() { hour = minute = second = 0; } 

The purpose of namespaces is to avoid name conflicts.

Later, compilation errors occur when calling functions in the cout stream, so you can break them down like this:

 int main () { my_code::time t; cout << "Initial universal time: "; t.printuniversal(); cout << "\nInitial standard time: "; t.printstandard(); t.settime(13,27,6); cout << "\nNew universal time: "; t.printuniversal(); cout << "\nNew standard time: "; t.printstandard(); return 0; } 

This is because these functions are returned invalid. An alternative is that these functions return std :: string.

Hope this offers an alternative understanding.

Greetings

Simon.

+3
source

Source: https://habr.com/ru/post/947937/


All Articles