static member problem in overloading delete operator

I defined my pool class(memory pool manage) as a static member in foo class as below but give error error : undefined reference to `foo::mem_' on operator delete.I tried to access mem_ with class foo qualifier but I can't figure it out!.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class foo{

static pool mem_; //this is the pool class which manage memory pool

create_array(row,col)
{
 ...  
}

delete_array()
{
 ...
}

void* operator new( size_t numSize )
{
	return mem_.allocate(numSize) ;
}

void operator delete( void *p )
{
 mem_.deallocate ( p,0) ; error : undefined reference to `foo::mem_'
}

}; 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class pool {

	enum pool_defaults{
		init_size =0x400,
		min_size = 0xf
	};

	public:
	pool(size_t size = init_size);
	virtual ~pool();

	void* allocate(size_t size);
	void deallocate(void *p, size_t s= 0);
.
.
.
};



1
2
3
4
5
6
7
8
9
10
11
12
13
int main(){

            foo cm;  // foo class

	clock_t start_time1=clock();
		for (int i = 0; i < itr; ++i) {
			B=cm.create_array(row,col);
			cm.delete_array(B);
		}
	clock_t end_time1=clock();

	cout<<((end_time1-start_time1)/(double) CLOCKS_PER_SEC)<<endl;
}

Last edited on
Becuase _mem is a static member, you must define it like this:

1
2
3
4
class foo { /* ... */ };

pool foo::mem_; // Eventually call a constructor for mem_ here if you want
Last edited on
I should add pool foo::mem_ at the end of my class I means in foo.cpp or foo.h . I ask this becuase befor your answer I add it above my main and it solve the problem.so I want to know if there is any difference or not?

Thanks
Last edited on
You should put it inside one of your source files because you only want to define it in one translation unit. If you put it's defined in more than one translation unit you will get an error message.
Topic archived. No new replies allowed.