Linked Questions
23 questions linked to/from Why does a static data member need to be defined outside of the class?
0 votes
2 answers
1k views
Why static members need to be initialized out of the class [duplicate]
#include "stdafx.h" #include <iostream> class X { public: static int n; }; int X::n; // out-of-class initialization int _tmain(int argc, _TCHAR* argv[]) { X x; std::cout <&...
2 votes
1 answer
2k views
Once more: why cannot static member variables be declared inline? [duplicate]
I know this question has already been asked, but googling around I didn't find a real answer to it. What I mean is: static member functions (and also non-static ones, for that matter) can be defined ...
1 vote
2 answers
198 views
Static variable declaration and definition [duplicate]
Consider the following code: #include "stdafx.h" #include <iostream> class Eelement { public: static int m_iVal; }; int Eelement::m_iVal = 33; int main() { //... return 0; } ...
1 vote
0 answers
179 views
Why does std::make_shared cause linker errors when using non-inline static const members? [duplicate]
I'm using C++17 and stumbled on a linker error with this kind of code #include <memory> struct SomeType { static const int MIN = 0; static const int MAX = 0; }; struct Range { ...
1 vote
0 answers
80 views
Why can't static members be declared in a block? [duplicate]
Here is some code: class aclass { int x = 1; static float peanut; //definition not allowed here or in any other class }; float aclass::peanut = 5.2; //definition seems to only be allowed at ...
1 vote
1 answer
77 views
Why does adding static data member result in linker failure? [duplicate]
Attempted to build a doubly linked list and printing it out. However, I received a linker command failed after adding "static Node* lastAdded". Not sure what the reason is. Also, for the head node, I ...
0 votes
0 answers
54 views
Static member within a class template [duplicate]
I was coding a class template to implement a Singleton when an issue occured. While having a static pointer in a .h file, it wouldnt compile because of a linker issue (lnk 2001 and lnk 1120 on vs 15)....
0 votes
0 answers
45 views
Singleton *Singleton::obj = 0; What does that line do in Singleton pattern implementation? [duplicate]
This code works fine as an implementation of Singleton pattern. I want to understand Singleton *Singleton::obj = 0;. What is that doing and why is that important? Please explain. #include <...
0 votes
0 answers
38 views
is there something wrong with my c++ singleton design pattern code? [duplicate]
using namespace std; class singleton_l //lazy { private: singleton_l(){cout << "lazy singleton created" << endl;} ~singleton_l(){} static ...
1 vote
0 answers
33 views
Static variables linker error: Undefined symbols for architecture x86_64 [duplicate]
I'm using Xcode 9.0 for a school assignment, a simple command line program. Getting these odd errors related to static class vars in my one class. I have read several similar questions and have tried ...
105 votes
3 answers
123k views
C++11 allows in-class initialization of non-static and non-const members. What changed?
Before C++11, we could only perform in-class initialization on static const members of integral or enumeration type. Stroustrup discusses this in his C++ FAQ, giving the following example: class Y { ...
34 votes
6 answers
47k views
Defining static members in C++
I am trying to define a public static variable like this : public : static int j=0; //or any other value too I am getting a compilation error on this very line : ISO C++ forbids in-...
26 votes
4 answers
6k views
Why do non-constant static variables need to be initialized outside the class? [duplicate]
I know that non-constant static variables need to be initialized outside the class definition but, is there a reason for this? class A { static int x = 0 // compile error; static int y; }; ...
29 votes
5 answers
5k views
Why static variable needs to be explicitly defined?
In the class: class foo { public: static int bar; //declaration of static data member }; int foo::bar = 0; //definition of data member We have to explicitly define the static variable, otherwise ...
28 votes
3 answers
2k views
Confusion about declaration and definition of static const data memebers
Scott Meyers writes in Effective Modern C++ (Item 30, at page 210) that there's no need to define integral static const data members in classes; declarations alone suffice, then the sample code is ...