Why do i get error :Undefined reference to Class::static member_variable?[Solved] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why do i get error :Undefined reference to Class::static member_variable?[Solved]

https://code.sololearn.com/cmTBam67531R/?ref=app

17th Aug 2021, 4:04 PM
Abhay
Abhay - avatar
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
17th Aug 2021, 4:30 PM
Shadow
Shadow - avatar
17th Aug 2021, 4:09 PM
Slick
Slick - avatar
+ 1
Slick thank you . I should have mentioned that i am trying to access variables inside static function.
17th Aug 2021, 4:36 PM
Abhay
Abhay - avatar
0
Slick i am trying to access static member of class
17th Aug 2021, 4:13 PM
Abhay
Abhay - avatar
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.
17th Aug 2021, 4:35 PM
Abhay
Abhay - avatar