Initialize static const string in class

Asked 1 years ago, Updated 1 years ago, 136 views

Isn't it okay to initialize the static const member variable within the class? The error message says to initialize out of line.

I don't want to use #define but what should I do

Static data member of type 'const string' (aka 'const basic_string<char, char_traits<char>, allocator<char> >') must be initialized out of line member

class A {
   private:
      static const string RECTANGLE = "rectangle";
}

class string c++ const static

2022-09-22 13:57

1 Answers

static const If a member variable can be initialized within the class

It is only possible for . If you ask, it should be initialized outside of the class because it is string.

yourheader.h)

class A {   
private:      
  static const string RECTANGLE;
};

yoursource.c)

const string A::RECTANGLE = "rectangle";


2022-09-22 13:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.