dimanche 24 mars 2019

What does this compiler error means - "qualified-id in declaration before ‘=’ token" in C++?

I am trying to understand the usage of private const in the class. My understanding is that private const is used to make something constant within the class and static to have one copy.

Initially, my code was using pi and it's data type was float. So, I tried changing the float to int because I read const static is only allowed for int types within class.

#include <iostream>
class MyExample
{

 private:
   static const int x;

};

int main(void)
{
  int const  MyExample::x = 3;

  std::cout<<"x value is "<<MyExample::x<<std::endl;

  return 0;
}

compiling -

$g++ -std=c++14 test.cpp
test.cpp: In function ‘int main()’:
test.cpp:12:27: error: qualified-id in declaration before ‘=’ token
   int const  MyExample::x = 3;

I know that moving the line " int const MyExample::x = 3;" from main() to outside, removes error if I remove private also.

Aucun commentaire:

Enregistrer un commentaire