Linked Questions
13 questions linked to/from How to initialize a "static const" data member in C++?
0 votes
2 answers
105 views
How to initialize static const members of a class which has the same type as the class? [duplicate]
I have a class whose static const members have the same type as the class. I am getting an error that class isn't yet complete. #include<iostream> class Color { public: Color(unsigned char ...
147 votes
5 answers
197k views
Where should a static const data member be defined? [duplicate]
I have a class class foo { public: foo(); foo( int ); private: static const string s; }; Where is the best place to initialize the string s in the source file?
7 votes
3 answers
18k views
C++ inline initialization of a static const char*
Why I cannot initialize a static const char* in the header file? In my code I have in my Class header: static const char* xml_ID_TAG; and in the cpp: const char* Class::xml_ID_TAG = "id"; The ...
3 votes
3 answers
5k views
C++ non-static const member in class without a constructor
I keep getting this error: warning: non-static const member ‘const char sict::Weather::_date [7]’ in class without a constructor [-Wuninitialized] And i don't understand it. main.cpp #include &...
5 votes
1 answer
1k views
Wrong static const initialization that compiles and works
As far as I know, you can only initialize static const members in the same line of their declaration if they are integral types . However, I was still able to initialize and use some static const ...
1 vote
2 answers
3k views
Can I initialize static const class member?
I want to run this code: struct A { static const string d[] = {"1", "2"}; }; And get an error: error: in-class initialization of static data member ‘const string A::d []’ ...
2 votes
2 answers
288 views
G++ (C++14) linker error on working C++03 code
Consider the following piece of code. class aClass { public: static const int HALLO = -3; }; int main() { std::vector<double > a; std::vector<int> b; std::vector<int> c; ...
2 votes
1 answer
2k views
Where to initialize static const member in c++17 or newer?
The questions is where to initialize static const member in c++17 or newer? Please consider the following two solutions for initializing a static const member in c++: Solution 1 (for c++14 or older): ...
1 vote
2 answers
603 views
Template class with std::enable_if_t, static const member initialization
Okay, after searching myself half to death and not finding any answer that actually seemed to work I gotta ask: Say I've got a class (contrived example, but hopefully good enough for now) template &...
2 votes
0 answers
268 views
How to implement static class members in a header-only library? [duplicate]
I am implementing a new header-only Library in VS2019, targeting C++11. As discussed (How to initialize a static const member in C++?) you cannot initialize a non-int static class member in-line. ...
1 vote
1 answer
135 views
Initialize static const member of a class, where the member is of a private type?
// gamewindow.hpp #include <SFML/Graphics.hpp> #include <string> #include <cstdint> class GameWindow : public sf::RenderWindow { public: GameWindow(const uint32_t&, const ...
0 votes
1 answer
118 views
Why a linker error concerning a static constant depends on optimization level?
I'm using std::make_unique (std::make_shared) to instantiate a class that takes a parameter. This parameter shall be initialized by a static constant. If this static constant is initialized by a ...
2 votes
0 answers
67 views
Undefined reference to static const member [duplicate]
This problem of mine was reproduced on cpp.sh with C++14 activated (I don't know if that is relevant...) I have this program: // Example program 1 #include <iostream> #include <vector> ...