This question already has answers here:
Undefined reference to a static member of the class
(2 answers)
Closed 7 years ago.
I have a code like this. When run, it's return fault: undefined reference to A::x. How can I fix this?
#include <iostream>
using namespace std;
class A
{
private:
static int x;
public:
A(){
}
A(int t) {
x = t;
}
static void f() {
cout<< A::x;
}
int f2() {
return x;
}
};
int main() {
A::f();
A a;
a.f2();
}
You have only declared your static variable, not defined it.
Define it outside of the class using:
int A::x = 0;
You need to add
int A::x = 0; //Or any other value
somewhere outside your class declaration.
Related
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..
This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Why not non-const reference to temporary objects? [duplicate]
(4 answers)
Closed 4 years ago.
#include <iostream>
using namespace std;
int f()
{
int x=1;
return x;
}
int main()
{
const int& s = f();
cout << s << endl;
}
#include <iostream>
using namespace std;
int x=1;
int &f()
{
return x;
}
int main()
{
const int& s = f();
cout << s << endl;
}
Both of these programs are correct. But when I use
int &f()
{
int x=1;
return x;
}
instead of
int f()
{
int x=1;
return x;
}
I get an error:
main.cpp: In function 'int& f()':
main.cpp:6:13: warning: reference to local variable 'x' returned [-Wreturn-local-addr]
int x=1;
^
bash: line 7: 14826 Segmentation fault (core dumped) ./a.out
What's wrong?
int &f()
{
int x=1;
return x;
// x reside on the function calling stack and it is temporary
// when you return the reference to x, after the function has returned,
// x is not there anymore(local variable)
}
if you really want to return a reference to a variable declared inside a function, consider allocate it on the heap, or declare it as a static variable
int &f()
{
int* x= new int;
*x = 1;
return *x;
}
int main(){
int& a = f();
cout << a; // 1
delete &a;
// and MAKE SURE you delete it when you don't need it
}
This question already has answers here:
Nontype template parameter
(3 answers)
Closed 8 years ago.
I don't understand why this works:
#include <iostream>
template<int& obj>
void foo() { obj = 42; }
int i;
int main()
{
foo<i>();
std::cout << i;
}
and that doesn't:
#include <iostream>
template<int& obj>
void foo() { obj = 42; }
int main()
{
int i;
foo<i>();
std::cout << i;
}
//error: the value of 'i' is not usable in a constant expression
The address of the local variable is a run-time feature, the address of the static variable is a compile time feature.
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.
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;
}