0

I am not able to use structures for static variable and function. Could anyone explain why exactly is this behavior?

#include <iostream>  
using namespace std;

class Foo
{
 private:
   typedef struct
   {
       int bee_1;
   }bee;
   static bee test;

 public:
   static void Inc(){ test.bee_1++;}
   static int getBee_1(){return test.bee_1;}

};

int main(){
  Foo::Inc();
  Foo::Inc();
  Foo::Inc();
  Foo::Inc();
  cout << Foo::getBee_1();
  return 0;
}

I get the following error when I use this code:

clang++-7 -pthread -o main main.cpp
/tmp/main-521333.o: In function `Foo::Inc()':
main.cpp:(.text._ZN3Foo3IncEv[_ZN3Foo3IncEv]+0x24): undefined reference to `Foo::test'
main.cpp:(.text._ZN3Foo3IncEv[_ZN3Foo3IncEv]+0x2e): undefined reference to `Foo::test'
/tmp/main-521333.o: In function `Foo::getBee_1()':
main.cpp:(.text._ZN3Foo8getBee_1Ev[_ZN3Foo8getBee_1Ev]+0x7): undefined reference to `Foo::test'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
compiler exit status 1
Skanda
  • 145
  • 4
  • 14
  • See in particular the last point of [this answer](https://stackoverflow.com/a/12574407/1782465) in the duplicate question – Angew is no longer proud of SO Sep 10 '19 at 10:58
  • If you look at the right column, you will see a list of "Releated" questions. One of them is named [static variable link error](https://stackoverflow.com/questions/9282354/static-variable-link-error?rq=1), and explains your problem. Stack Overflow is also kind enough to display a list of related questions when you write your question. Please look through it next time. – Some programmer dude Sep 10 '19 at 11:00

0 Answers0