Good day. After compiling the code, an unresolved externals error pops up.

class Name { public: static int a; }; void main() { Name::a = 5; cout << Name::a << endl; system("pause"); } 

Why? Deytel seems to say so

2 answers 2

You can, just need to write correctly

 #include <iostream> using namespace std; class Name { public: static int a; }; int Name::a = 5; int main() { cout << Name::a << endl; return 0; } 

    An error in the first line of main when referring to a declared but not defined static member of a class actually takes place (I initially overlooked it, the downsides in my direction are completely justified).

    Secondly, try not to use the system ("pause") to stop the execution of the program, this is a system- dependent, unbearable, non- recommended method. For this purpose it is better to use the standard std :: cin.get () tool as follows:

     #include <iostream> class Name { public: static int a; }; int Name::a = 5; int main() { Name::a = 5; std::cout << Name::a << std::endl; std::cin.get(); // system("pause"); } 
    • std::cin.get() also not needed. The console application either runs directly in the console, then such an overkill is not needed, or with a debugger, and there is a stub for this. - KoVadim
    • @KoVadim: I did not try to judge the tasks facing the author of the question, but proceeded from the fact that if a person writes system ("pause"), then he probably has a specific goal. By the way, I occasionally encounter a similar code waiting for input at the end. I never understood the meaning of this, but perhaps this is due to the fact that in Windows (with default settings) the console window closes immediately upon completion of the process that opened it. - cridnirk
    • read between the lines - "I copied the code from the book of Deythel, it was of an unknown year and OCR was made by a person for whom only Chinese is native." Continue to continue? - KoVadim