0
#include <utility>
class C {
   private:
     const std::pair<int,int> corner1(1,1);
};

GCC reports error: expected identifier before numeric constant.

I need to construct the object on the moment of it's declaration since it's const, but I can't seem it get the right syntax.

Patrick.SE
  • 4,444
  • 5
  • 34
  • 44

2 Answers2

1

I need to construct the object on the moment of it's declaration since it's const, but I can't seem it get the right syntax.

No, you can only initialize non-integral types - const or not (at least pre-C++11) in the constructor initializer list:

class C {
   private:
     const std::pair<int,int> corner1;
     C() : corner1(1,1) {}
};

But it seems to me like you don't need to replicate the member in every instance, so I'd just make it static instead:

class C {
   private:
     static const std::pair<int,int> corner1;
};

//implementation file:
const std::pair<int,int> C::corner1(1,1);
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
0

If you pass -std=c++11 and you are using a more recent version of gcc, you can do this:

class C {
   private:
     const std::pair<int,int> corner1{1,1}; // Note curly braces
};
Jesse Good
  • 50,901
  • 14
  • 124
  • 166