7
 //foo.h
 class Foo 
  {
    private:
      static int number;

    public: 
      static int bar();
  };

//foo.cc
#include "foo.h"

 int Foo::bar() 
 {
   return Foo::number;
 }

this is not working. I want to define a static function outside the class definition and access a static value.

undefined reference to `Foo::number'
thomas050903
  • 81
  • 1
  • 4

3 Answers3

8

You just declared the static member you need to define it too. Add this in your cpp file.

int Foo::number = 0;

This should be a good read:

what is the difference between a definition and a declaration?

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
2

you have to define Foo::number:

// foo.cc
...
int Foo::number(0);
justin
  • 104,054
  • 14
  • 179
  • 226
1

You have declared Foo::number you have to add a definition. In your cpp file Add this line

int Foo::number = 0;  
rerun
  • 25,014
  • 6
  • 48
  • 78