+ 1
Why do i get error :Undefined reference to Class::static member_variable?[Solved]
5 Answers
+ 8
You only declared the variable, but you never actually define it. Add the following definitions after the class:
int num::positive_num = 0;
int num::negative_num = 0;
As an alternative, you can use the inline attribute to define static members directly inside the class (as of C++17, I think):
inline static int positive_num = 0;
inline static int negative_num = 0;
See the following link for more details:
https://en.cppreference.com/w/cpp/language/static
+ 3
It's not declared as an int
https://code.sololearn.com/cQ88RpQ0D4NI/?ref=app
+ 1
Slick thank you . I should have mentioned that i am trying to access variables inside static function.
0
Slick i am trying to access static member of class
0
Shadow thanks a lot .Previously i declared the variable but inside the class and then it gave me other error .
Looks cumbersome to define them outside of class declaration.
inline seems good .
Edit: I went through so many tutorials such as,
https://www.google.com/amp/s/www.edureka.co/blog/what-is-static-member-function-in-cpp/amp/
but never came across something like inline.