//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'
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?
you have to define Foo::number
:
// foo.cc
...
int Foo::number(0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With