"undefined reference" to static members

I am practising the use of static members. I want to have the main file to deal with two headers.

First header:
1
2
3
4
5
6
7
8
9
10
11
#ifndef _FOO_HPP_
#define _FOO_HPP_

class foo{
int _i;
public:
foo(int i): _i(i) {;}
int get_i() {return _i;}
};

#endif 


Second header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef _BAR_HPP_
#define _BAR_HPP_

#include "foo.hpp"

class bar{
public:
static int j;
static foo f;

static foo fooval() {j++; return f;}  // record how many times fooval has been called and return the static member f
static int callsfooval() {return j;}  // return how many times fooval has been called
};

#endif 


The main program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include "foo.hpp"
#include "bar.hpp"

int main()
{
bar g;
int j = 0;
foo f(1);

std::cout << f.get_i() << std::endl;
std::cout << g.fooval().get_i() << std::endl;
std::cout << g.callsfooval() << std::endl;
return 0;
}


When I tried to compile, I was bounced back by

/tmp/ccjjQOyJ.o: In function `bar::fooval()':
cp12_38.cpp:(.text._ZN3bar6foovalEv[bar::fooval()]+0x8): undefined reference to `bar::j'
cp12_38.cpp:(.text._ZN3bar6foovalEv[bar::fooval()]+0x11): undefined reference to `bar::j'
cp12_38.cpp:(.text._ZN3bar6foovalEv[bar::fooval()]+0x17): undefined reference to `bar::f'
/tmp/ccjjQOyJ.o: In function `bar::callsfooval()':
cp12_38.cpp:(.text._ZN3bar11callsfoovalEv[bar::callsfooval()]+0x4): undefined reference to `bar::j'
collect2: ld returned 1 exit status


Can anyone tell what these errors mean? How should I revise my codes? Thank you!
Last edited on
static data members are only declared inside the class definition so you have to put the definition in a source file. So to define j you write int bar::j; in a source file. You can also give j another starting value here if you like. int bar::j = 10;
Hi Peter87,

Thanks for your help. I tried what you suggested in the main program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include "foo.hpp"
#include "bar.hpp"

int main()
{
bar g;
int bar::j = 0;
foo bar::f(1);

std::cout << f.get_i() << std::endl;
std::cout << g.fooval().get_i() << std::endl;
std::cout << g.callsfooval() << std::endl;
return 0;
}

Here are the new errors.

cp12_38.cpp: In function ‘int main()’:
cp12_38.cpp:10: error: invalid use of qualified-name ‘bar::j’
cp12_38.cpp:11: error: invalid use of qualified-name ‘bar::f’
cp12_38.cpp:13: error: ‘f’ was not declared in this scope


I'm still confused. Any comments or hints? Thanks a lot.
Last edited on
try putting the definitions of j and foo outside main.
I tried to put the definitions of j and foo before main function. But it still throws error:

cp12_38.cpp: In function ‘int main()’:
cp12_38.cpp:13: error: ‘f’ was not declared in this scope
bump up~
On global scope (the best place is on bar.cpp):

1
2
int bar::j = 1;
foo bar::foo(1);
bbgst,

Thanks for what you suggested. But it even made the thing worse. I really doubt if these two initialization should appear in class bar (or appear somewhere outside the class but in bar.cpp).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class foo
{
	int _i;

	public:
		foo(int i) : _i(i) {}
};

class bar
{
	public:
		static int j;
		static foo f;
};

int bar::j = 0;
foo bar::f(1);
It works now. Thanks, bbgst!
Topic archived. No new replies allowed.