0

I'm trying to understand what the compiler complains and how to follow rules by C++. I very appreciate any help

#include <string>
struct Foo {
  static const char* BAR = "Bar"; // -> Not okay
  static constexpr const char* FOO = "Foo"; // okay
  static std::string s_; //-> okay
  static constexpr std::string s("sss"); // not okay
};

std::string Foo::s_ = "sss";

int main()
{
    return 0;
}

Out:

error: ‘constexpr’ needed for in-class initialization of static data member ‘const char* Foo::BAR’ of non-integral type [-fpermissive]
   static const char* BAR = "Bar"; // -> Not okay
                      ^~~
test.cc:6:40: error: expected identifier before string constant
   static constexpr std::string s("sss"); // not okay
                                        ^~~~~
test.cc:6:40: error: expected ‘,’ or ‘...’ before string constant
nhatnq
  • 1,173
  • 7
  • 16

0 Answers0