0

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.

1
  • 1
    Well as compiler said, you have declared k twice, one at static int k; and another at int k and, as in the example that you have linked int X::i = 0; // definition outside class declaration but not inside main() function
    – Amadeus
    Commented Mar 25, 2019 at 2:04

1 Answer 1

2

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.

Yes, the static int k; inside the class definition is indeed just a declaration of the static data member and not a definition. And yes, you need to provide a definition of the static data member somewhere. That somewhere cannot be inside the main function, or any function for that matter. And yes, it has nothing to do with private vs public. This is just about where a definition of a static data member can occur. Definitions of static data members (that are not defined inline) have to reside in a namespace scope enclosing the class definition. Because the rules of the C++ language say so (specifically, in [class.static.data]/2). The global namespace is one such place where a static data member can be defined…

5
  • 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 why it needs to be global then? It has nothing to do with private/public in my opinion. Commented Mar 25, 2019 at 2:19
  • Yes, the static int k; inside the class definition is indeed just a declaration of the static data member and not a definition. And yes, you need to provide a definition of the static data member somewhere. That somewhere cannot be inside the main function, or any function, however. Definitions of static data members (that are not defined inline) have to reside in a namespace scope enclosing the class definition ([class.static.data]/2). The global namespace is one such place where a static data member can be defined… Commented Mar 25, 2019 at 2:25
  • @MichaelKenzel Add that comment to your question. I was about to answer before I saw that and I'm lazy enough to rather press the upvote button than write an answer. Yes, I know Prawn stepped outside the bounds of good taste by changing the point of the question, but you have to admit the first version of the question was't particularly good. Commented Mar 25, 2019 at 3:44
  • @user4581301 thanks for the poke, I didnt even notice that the question was edited anymore. Updated my answer… Commented Mar 25, 2019 at 4:09
  • what's the use of private here? Why somebody will mark it - private. I am going through Scott Myers - Effective C++ and it says use private: static const int k = 10; I am confused. Is putting const means it's no more declaration but a definition. Commented Mar 25, 2019 at 19:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.