Using map<> containers - yet more problems !

This is really a continuation of my previous post regarding maps. I'm having problems using a map, my compiler g++ complains it cannot find a reference to a map decleration. I've had to write some test modules just to hightlight the problem and post them here :

test.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <map>
#include <utility>

typedef std::map<int,int> testMap;

class Test{
public:
Test();
virtual ~Test();

static void TestMe();
private:

static testMap tm;
};


test.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "test.hpp"

Test::Test(){
};

Test::~Test(){
};

void Test::TestMe(){
	tm.insert(std::pair<int,int>(1,2));
};


main.cpp
1
2
3
4
5
6
7
8
#include "test.hpp"

int main(int argc, char** argv) {
	Test* t = new Test();
	t->TestMe();
	delete(t);
return(0);
}


When I try to compile using g++ and the command 'g++ -o test main.cpp test.cpp
'

I get this compile error :
/tmp/ccgEDjJB.o: In function `Test::TestMe()':
test.cpp:(.text+0x69): undefined reference to `Test::tm'
collect2: ld returned 1 exit status

Just what is wrong ? I really cannot work this one out.. is it the way I'm declaring a map ?

Your help appreciated
In test.cpp you need to instantiate tm. You only declare it on line 14 in the header.

jsmith,

I've declared tm as static, so I understand that means I can't instantiate it , or have I got that wrong.

I need it as static as I only want one copy of the map container for a class.. does it mean I can't use a map as static ? Seems a bit daft if that's the case.
Static members must be defined externally:
testMap Test::tm;
Bazzy !

cheers mate. problem solved. I just didn't realise that had to be done with static members !

thanks once again
Topic archived. No new replies allowed.