Hi i am getting undefined reference error in the following code:
class Helloworld{
public:
static int x;
void foo();
};
void Helloworld::foo(){
Helloworld::x = 10;
};
I don't want a static foo() function. How can I access static variable of a class in non-static method of a class?
I don't want a static foo() function
Well, foo() is not static in your class, and you do not need to make it static in order to access static variables of your class.
What you need to do is simply to provide a definition for your static member variable:
class Helloworld {
public:
static int x;
void foo();
};
int Helloworld::x = 0; // Or whatever is the most appropriate value
// for initializing x. Notice, that the
// initializer is not required: if absent,
// x will be zero-initialized.
void Helloworld::foo() {
Helloworld::x = 10;
};
The code is correct, but incomplete. The class Helloworld has a declaration of its static data member x, but there is no definition of that data member. Somehwere in your source code you need
int Helloworld::x;
or, if 0 isn't an appropriate initial value, add an initializer.
Old question, but;
Since c++17 you can declare static members inline and instantiate them inside the body of class without the need of an out-of-class definition:
class Helloworld{
public:
inline static int x = 10;
void foo();
};
Related
Hi i am getting undefined reference error in the following code:
class Helloworld{
public:
static int x;
void foo();
};
void Helloworld::foo(){
Helloworld::x = 10;
};
I don't want a static foo() function. How can I access static variable of a class in non-static method of a class?
I don't want a static foo() function
Well, foo() is not static in your class, and you do not need to make it static in order to access static variables of your class.
What you need to do is simply to provide a definition for your static member variable:
class Helloworld {
public:
static int x;
void foo();
};
int Helloworld::x = 0; // Or whatever is the most appropriate value
// for initializing x. Notice, that the
// initializer is not required: if absent,
// x will be zero-initialized.
void Helloworld::foo() {
Helloworld::x = 10;
};
The code is correct, but incomplete. The class Helloworld has a declaration of its static data member x, but there is no definition of that data member. Somehwere in your source code you need
int Helloworld::x;
or, if 0 isn't an appropriate initial value, add an initializer.
Old question, but;
Since c++17 you can declare static members inline and instantiate them inside the body of class without the need of an out-of-class definition:
class Helloworld{
public:
inline static int x = 10;
void foo();
};
Hi i am getting undefined reference error in the following code:
class Helloworld{
public:
static int x;
void foo();
};
void Helloworld::foo(){
Helloworld::x = 10;
};
I don't want a static foo() function. How can I access static variable of a class in non-static method of a class?
I don't want a static foo() function
Well, foo() is not static in your class, and you do not need to make it static in order to access static variables of your class.
What you need to do is simply to provide a definition for your static member variable:
class Helloworld {
public:
static int x;
void foo();
};
int Helloworld::x = 0; // Or whatever is the most appropriate value
// for initializing x. Notice, that the
// initializer is not required: if absent,
// x will be zero-initialized.
void Helloworld::foo() {
Helloworld::x = 10;
};
The code is correct, but incomplete. The class Helloworld has a declaration of its static data member x, but there is no definition of that data member. Somehwere in your source code you need
int Helloworld::x;
or, if 0 isn't an appropriate initial value, add an initializer.
Old question, but;
Since c++17 you can declare static members inline and instantiate them inside the body of class without the need of an out-of-class definition:
class Helloworld{
public:
inline static int x = 10;
void foo();
};
Hi i am getting undefined reference error in the following code:
class Helloworld{
public:
static int x;
void foo();
};
void Helloworld::foo(){
Helloworld::x = 10;
};
I don't want a static foo() function. How can I access static variable of a class in non-static method of a class?
I don't want a static foo() function
Well, foo() is not static in your class, and you do not need to make it static in order to access static variables of your class.
What you need to do is simply to provide a definition for your static member variable:
class Helloworld {
public:
static int x;
void foo();
};
int Helloworld::x = 0; // Or whatever is the most appropriate value
// for initializing x. Notice, that the
// initializer is not required: if absent,
// x will be zero-initialized.
void Helloworld::foo() {
Helloworld::x = 10;
};
The code is correct, but incomplete. The class Helloworld has a declaration of its static data member x, but there is no definition of that data member. Somehwere in your source code you need
int Helloworld::x;
or, if 0 isn't an appropriate initial value, add an initializer.
Old question, but;
Since c++17 you can declare static members inline and instantiate them inside the body of class without the need of an out-of-class definition:
class Helloworld{
public:
inline static int x = 10;
void foo();
};
Hi i am getting undefined reference error in the following code:
class Helloworld{
public:
static int x;
void foo();
};
void Helloworld::foo(){
Helloworld::x = 10;
};
I don't want a static foo() function. How can I access static variable of a class in non-static method of a class?
I don't want a static foo() function
Well, foo() is not static in your class, and you do not need to make it static in order to access static variables of your class.
What you need to do is simply to provide a definition for your static member variable:
class Helloworld {
public:
static int x;
void foo();
};
int Helloworld::x = 0; // Or whatever is the most appropriate value
// for initializing x. Notice, that the
// initializer is not required: if absent,
// x will be zero-initialized.
void Helloworld::foo() {
Helloworld::x = 10;
};
The code is correct, but incomplete. The class Helloworld has a declaration of its static data member x, but there is no definition of that data member. Somehwere in your source code you need
int Helloworld::x;
or, if 0 isn't an appropriate initial value, add an initializer.
Old question, but;
Since c++17 you can declare static members inline and instantiate them inside the body of class without the need of an out-of-class definition:
class Helloworld{
public:
inline static int x = 10;
void foo();
};
Hi i am getting undefined reference error in the following code:
class Helloworld{
public:
static int x;
void foo();
};
void Helloworld::foo(){
Helloworld::x = 10;
};
I don't want a static foo() function. How can I access static variable of a class in non-static method of a class?
I don't want a static foo() function
Well, foo() is not static in your class, and you do not need to make it static in order to access static variables of your class.
What you need to do is simply to provide a definition for your static member variable:
class Helloworld {
public:
static int x;
void foo();
};
int Helloworld::x = 0; // Or whatever is the most appropriate value
// for initializing x. Notice, that the
// initializer is not required: if absent,
// x will be zero-initialized.
void Helloworld::foo() {
Helloworld::x = 10;
};
The code is correct, but incomplete. The class Helloworld has a declaration of its static data member x, but there is no definition of that data member. Somehwere in your source code you need
int Helloworld::x;
or, if 0 isn't an appropriate initial value, add an initializer.
Old question, but;
Since c++17 you can declare static members inline and instantiate them inside the body of class without the need of an out-of-class definition:
class Helloworld{
public:
inline static int x = 10;
void foo();
};