in Education by
I am following simple examples to build up my understanding on private static members. However, I am getting compilation errors. Here is the example, I am following - https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.1.0/com.ibm.zos.v2r1.cbclx01/cplr038.htm have changed the c++ version to see if it something to do with c++ version. class test1 { static int i; static int j; static int k; static int f() { return 0;} int p; public: test1() { p = 0; } }; int main(void) { test1 t; int test1::i = test1::f(); int test1::j = test1::i; return 0; } Compiling this code - Why does it fail here whereas the example link says it works. $g++ -std=c++14 t1.cpp -o t1.out t1.cpp: In function ‘int main()’: t1.cpp:19:16: error: qualified-id in declaration before ‘=’ token int test1::i = test1::f(); ^ t1.cpp:20:16: error: qualified-id in declaration before ‘=’ token int test1::j = test1::i; It works if I do like this - class test1 { static int i; static int j; static int k; static int f() { return 0;} int p; public: test1() { p = 0; } }; int test1::i = test1::f(); int test1::j = test1::i; int main(void) { test1 t; return 0; } Why it has to be defined outside of main() ? It works with private also. However, it cannot be defined inside main function. My understanding is that static int inside class is just a declaration. It needs to be defined somewhere. I am defining it within main() but it doesn't work. It needs to be global and I don't know why it needs to be global? It has nothing to do with private/public in my opinion. Please clarify my doubt here. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

Related questions

0 votes
    During a long compilation with Visual Studio 2005 (version 8.0.50727.762), I sometimes get the following ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    During a long compilation with Visual Studio 2005 (version 8.0.50727.762), I sometimes get the following ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    I would like to know if I can compare 2 member functions with the "...
asked Feb 27, 2022 in Education by JackTerrance
0 votes
    I would like to know if I can compare 2 member functions with the "...
asked Feb 27, 2022 in Education by JackTerrance
0 votes
    I would like to know if I can compare 2 member functions with the "...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    I would like to know if I can compare 2 member functions with the "...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    I'm relatively new to C++, recently moved from C# and Java (and before that, used to work in ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    Since I got multiple classes and utility-functions I reuse on a regular basis, I started to create a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 25, 2022 in Education by JackTerrance
0 votes
    Since I got multiple classes and utility-functions I reuse on a regular basis, I started to create a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    R uses _________ scoping^6 0 or static scoping. (a) reflective (b) transitive (c) lexical (d) closure ... Debugging of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    Write error message given by C compiler during compilation if we use %d to print float variable Select the correct answer from above options...
asked Nov 27, 2021 in Education by JackTerrance
0 votes
    Closed. This question needs debugging details. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    Closed. This question needs debugging details. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I am trying to understand the usage of private const in the class. My understanding is that private const is used to make ... (void) { int const MyExample::x = 3; std::cout...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    I wrote a program which gets matches between 2 pictures. And this is the code. but if I use ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
...