Pointer to static member variable [duplicate] - c++

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C++: undefined reference to static class member
The following C++ code compiles well (using g++ -c) but it doesn't link giving the error: undefined reference toAbc::X'`
#include <iostream>
using namespace std;
class Abc {
public:
const static int X = 99;
};
int main()
{
Abc a1;
cout << &(Abc::X) << endl;
}
I want to know why this is not allowed?

You need to have that static member actually defined, not just declared...
Add this line before your main():
const int Abc::X = 99;
As of C++17 you can also do an inline static, in which case the above additional line of code in a .cpp file is not needed:
class Abc {
public:
inline const static int X = 99; // <-- "inline"
};

If the static member is used in a way which requires an lvalue (i.e. in a way that requires it to have an address) then it must have a definition. See the explanation at the GCC wiki, which includes references to the standard and how to fix it.

If you don't like to think about translation units, static initialization order and stuff like that, just change your static constants into methods.
#include <iostream>
using namespace std;
class Abc {
public:
inline static const int& X(){
static int x=99;
return x;
}
};
int main()
{
// Abc a1;
cout << &(Abc::X()) << endl;
}

Related

Error LNK2001 in Visual Studio 2017 when using static int [duplicate]

This question already has answers here:
How to initialize private static members in C++?
(18 answers)
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 3 years ago.
I'm doing some practice tasks for uni and I'm supposed to create static int field inside a class, but when I do so I get error LNK2001. When I change it to regular int the error does not occure. Can anybody help me please? Here's my code:
#include <iostream>
#include <string>
using namespace std;
class Uczelnia {
public:
virtual string getName() = 0;
static int liczba_wszystkich_studentow;
};
class Politechnika:public Uczelnia {
public:
Politechnika(string a, int b) {
nazwa = a;
liczba_studentow = b;
liczba_wszystkich_studentow = +liczba_studentow;
}
string getName() {
cout << "Politechnika: " << nazwa << endl;
return nazwa;
}
~Politechnika() {
liczba_wszystkich_studentow = -liczba_studentow;
}
private:
string nazwa;
int liczba_studentow;
};
class Uniwersytet :public Uczelnia {
public:
Uniwersytet(string a, int b) {
nazwa = a;
liczba_studentow = b;
liczba_wszystkich_studentow = +liczba_studentow;
}
string getName() {
cout << "Uniwersytet: " << nazwa << endl;
return nazwa;
}
~Uniwersytet() {
liczba_wszystkich_studentow = -liczba_studentow;
}
private:
string nazwa;
int liczba_studentow;
};
int main() {
Politechnika p1("Warszawska", 200);
p1.getName();
Uniwersytet u1("Warszawski", 600);
}
You're getting a linker error because you haven't initialized the static member.
You just need to initialize it outside of the class.
class Uczelnia {
public:
//..
static int liczba_wszystkich_studentow;
//..
};
int Uczelnia::liczba_wszystkich_studentow = 5;
There are some additional intricacies of being able to initialize static const integral types (like int) inside of the class, but with others you would typically initialize these static members in the source file outside of the class definition.
Within a class definition there are declarations of static data members not their definitions. Declared static data members within a class definition may even have an incomplete type. If a static data member is ODR used it shall be defined outside a class definition in some module. For example
int Uczelnia::liczba_wszystkich_studentow;
In C++ 17 you can use the inline specifier in a declaration of a static data member within a class definition.
For example
class Uczelnia {
public:
virtual string getName() = 0;
inline static int liczba_wszystkich_studentow;
};
In this case the code will compile provided that the compiler supports C++ 17..

why i can't call a static member variable in an static member function like this?

everyone! there is a code snippet like the below:
testcase.cpp
#include <string>
#include <iostream>
using namespace std;
class Test {
public:
static int b ;
static void test()
{
b = 3;
cout << b<<endl;
}
};
int main()
{
Test::test();
return 0;
}
when I click "build" button,the output message is
error LNK2001: unresolved external symbol "public: static int Test::b" (?b#Test##2HA)
1>B:\PROGRAMPROJECT\visual 2015 pro\testcase\testcase\x64\Debug\testcase.exe : fatal error LNK1120: 1 unresolved externals
but,when i change the code location like this:
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
class Test {
public:
//static int b;
static void test()
{
static int b;//only change this line
b = 3;
cout << b << endl;
}
};
int main()
{
Test::test();
return 0;
}
It does work!
I don't know why?Anybody can help me?
My IDE is vs pro 2015 + windows10.
How to solve it
Using C++17 you can inline the static variable which removes the need to define it outside the class.
static inline int i = 0; // I would also initialize it to zero, just to be sure
If you cannot use C++17 you will have to define
int Test::b = 0; // Again I would also initialize it to zero, just to be sure
outside of class Test
To the why
When you wrote
static void test()
{
static int b; // <-- definition is here
b = 3;
cout << b << endl;
}
you defined the function directly in the class (which means it is automatically marked inline). You then also have the definition of your static variable right there. If it is outside of the function scope, it is only a declaration (except if explicitly marked as inline - as shown above).
In contrast, in the other case it is outside of the function definition and hence your are missing the definition of the static variable.
class Test {
public:
static int b ; // <-- only declaration - definition is missing
static void test()
{
b = 3;
cout << b<<endl;
}
};
Fix spelled out for C++17
class Test {
public:
static inline int b = 0; // <-- now it is defined (and for safety initialized)
static void test()
{
b = 3;
cout << b<<endl;
}
};
Fix spelled out prior to C++17
#include <string>
#include <iostream>
using namespace std;
class Test {
public:
static int b; // declaration
static void test()
{
b = 3;
cout << b << endl;
}
};
int Test::b = 0; // definition
int main()
{
Test::test();
return 0;
}
The other anwers focus on solving the problem, instead of answering the "why" part.
When you define a function within class body, it is automatically considered to be inline and any number of translation units (think about C++ sources) may define them. That's the way you would normally define functions within header file - a function that's body will be included in multiple sources.
When you only declare a function within class body, but define it outside, it is not automatically marked as inline, which means that each translation unit that contains this definition will generate its own strong symbol. In this case, you usually decouple the declaration in a header file, from implementation in an associated source file, otherwise you'd get multiple symbol errors. (i.e. if you have non-inline definitions in headers, each source that include them generates those symbols and linker gets confused because it sees multiple versions of the same non-inline function coming from different sources)
Now, when you define a static member variable, unless it's inline, it also must be also associated with a translation unit. It's similar to declaring an extern global variable, that also has to be defined somewhere. In your case, you should simply add:
int Test::b;
It's more important when you decouple definition from declaration. Say, you have two files:
Test.hpp
class Test {
public:
static int b ;
static void test();
};
Test.cpp
#include "Test.hpp"
int Test::b;
void Test::test()
{
b = 3;
cout << b<<endl;
}
The symbols (in this case, the global variable Test::b and the function Test::test) are associated with the file Test.cpp and are generated exactly once. You may include Test.hpp in any number of source files and no additional symbols will be generated.
For referece, see https://en.cppreference.com/w/cpp/language/static
You've declared Test::b but you haven't defined it. Add this line to your code (outside of the Test class)
int Test::b;
you can give an initial value as well if you wish
int Test::b = 123;

unable to access static methods (C++)

Here's the code:
#include <iostream>
using namespace std;
class Zaix
{
private:
static int mor;
public:
static int beri;
static void setmor(int lip)
{
Zaix::mor=lip;
}
static int getmor(void)
{
return mor;
}
};
int Zaix::beri=3;
int main()
{
cout<<Zaix::beri<<endl;
Zaix::beri++;
cout<<Zaix::beri<<endl;
Zaix::setmor(6);
return 0;
}
Now, line 4 of main() function Zaix::setmor(6); somehow invalidates line 11 of the code presented Zaix::mor=lip;. With this line commented out, the whole thing compiles OK, with it present, compiler gives this error:
undefined reference to Zaix::mor"
Any idea why that is?
Define the variable outside class as well.
int Zaix::mor;
For assignment:
int Zaix::mor = 4;
In C++ we need to define all the static member variable of a class outside of it else we get a linking error. You just need to do like below:-
int Zaix::mor;// Just add this line below int Zaix::beri = 3;

C++ static functions and variables

I have written a class as shown below:
#include<iostream>
using namespace std;
class A
{
static int cnt;
static void inc()
{
cnt++;
}
int a;
public:
A(){ inc(); }
};
int main()
{
A d;
return 0;
}
I want to call the function inc through the constructor, but when i compile i am getting an error as:
/tmp/ccWR1moH.o: In function `A::inc()':
s.cpp:(.text._ZN1A3incEv[A::inc()]+0x6): undefined reference to `A::cnt'
s.cpp:(.text._ZN1A3incEv[A::inc()]+0xf): undefined reference to `A::cnt'
I am unable to understand what the error is... plz help...
Static field is not defined - Take a look at Why are classes with static data members getting linker errors?.
#include<iostream>
using namespace std;
class A
{
static int cnt;
static void inc(){
cnt++;
}
int a;
public:
A(){ inc(); }
};
int A::cnt; //<---- HERE
int main()
{
A d;
return 0;
}
Inside the class static int cnt; is only declared, and need to be defined. In C++ you usually declare in your .h .hpp files and then define your static class members in your .c and .cpp files.
In your case, you need to add
int A::cnt=0; // = 0 Would be better, otherwise you're accessing an uninitialized variable.

How to access static members of a class?

I am starting to learn C++ and Qt, but sometimes the simplest code that I paste from a book results in errors.
I'm using g++4.4.2 on Ubuntu 10.04 with QtCreator IDE. Is there a difference between the g++ compiler syntax and other compilers? For example when I try to access static members something always goes wrong.
#include <iostream>
using namespace std;
class A
{
public:
static int x;
static int getX() {return x;}
};
int main()
{
int A::x = 100; // error: invalid use of qualified-name 'A::x'
cout<<A::getX(); // error: : undefined reference to 'A::x'
return 0;
}
I think it's exactly the same as declared here and here (isn't it?). So what's wrong with the above code?
You've declared the static members fine, but not defined them anywhere.
Basically what you've said "there exists some static member", but never set aside some memory for it, you need:
int A::x = 100;
Somewhere outside the class and not inside main.
Section [9.4.2]
Static Data Members
The declaration of a static data member in its class definition is not a definition and may be of an incomplete type other than cv-qualified void. The definition for a static data member shall appear in a namespace scope enclosing the member’s class definition. In the definition at namespace scope, the name of the static data member shall be qualified by its class name using the :: operator
You need to define the static member variable of the class outside the class as static member variables require declaration as well as definition.
#include <iostream>
using namespace std;
class A
{
public:
static int x;
static int getX() {return x;}
};
int A::x; // STATIC MEMBER VARIABLE x DEFINITION
int main()
{
A::x = 100; // REMOVE int FROM HERE
cout<<A::getX();
return 0;
}
Try:
#include <iostream>
using namespace std;
class A
{
public:
// This declares it.
static int x;
static int getX(){return x;}
};
// Now you need an create the object so
// This must be done in once source file (at file scope level)
int A::x = 100;
int main()
{
A::x = 200;
// Notice no 'int' keyword before A::x on this line. You can modify A::x
cout<<A::getX(); // Should work
return 0;
}
The definition of static member variables must live at file scope, i.e. outside all functions, etc.
Try this example:
#include<iostream>
using namespace std;
class check
{
static int a;
public:
void change();
} ;
int check::a=10;
void check::change()
{
a++;
cout<<a<<"\n";
}
int main()
{
int i,j;
check c;
check b;
c.change();
b.change();
return 0;
}
Now you have worked out how to use static class members I will advise you that you should generally use them only in the following circumstances:
For use in templates. So in your example you could have GetX() in different classes and in a template somewhere you would use
template< typename T >
int func()
{
return T::GetX();
}
although obviously more elaborate. But here your static function being in a class serves a purpose.
Where the function needs access to the class, i.e. to private members. You could make it a friend but you may as well make it static. Often the case in callbacks.
The rest of the time you can probably use compilation-unit level functions and variables which has the advantage of taking your members out of the header (particularly if they are private). The less implementation detail you give the better.
You can use the inline keyword since c++ 17 in front of static members to avoid a definition outside of class scope. Your code should now look like this:
#include <iostream>
using namespace std;
class A
{
public:
inline static int x;
static int getX() {return x;}
};
int main()
{
A::x = 100; //Works now
cout<<A::getX()<<'\n';
return 0;
}
Case 1: static variable
As we all know, defining a static variable inside a class which will throw compilation error. E.g. below
class Stats
{
public:
static int AtkStats[3];
*static int a =20;* // Error: defining a value for static variable
};
int Stats::AtkStats[3] = {10, 0, 0};
Output:
error: ISO C++ forbids in-class initialization of non-const static member 'Stats::a'
Case 2: const static variable
For const static variable, we can define a value either inside a class or Outside class.
class Stats
{
public:
static const int AtkStats[3];
static const int a =20; // Success: defining a value for a const static
};
const int Stats::AtkStats[3] = {10, 0, 0};
const int Stats::a = 20; // we can define outside also
Output:
Compilation success.