SWL8

I am trying to create a map variable (called room_list) in my class (called building) - code below. When I run my code, i get two errors - listed below. Can anyone help me

Errors:

.\building.cpp(121) : error C2065: 'room_list' : undeclared identifier

.\building.cpp(121) : error C2228: left of '.insert' must have class/struct/union

Code:

class building

{

int id;

typedef std::map<int,room> room_list;

public:

building(int id);

~building(void);

void determineRoom(void);

};

building::building(int b_id)

{id = b_id;}

void determineRoom(void)

{

int r_id[3] = {1, 2, 3};

for (int i = 0; i < 3; i++)

{

room newRoom(r_idIdea);

room_list.insert(pair<string, room>(newRoom.idToString(), newRoom));

}

}



Re: Visual C++ Language Using/Creating a MAP variable as a Class member

Shakje

room_list is just a typedef not a variable. This means it acts like an alias for std::map<int,room> like using a #define in some ways, so that you don't have to type that out every time you want to create a variable of that type. It is not a variable, and you cannot access it as such which is why you get errors.

Either don't use typedef there (std::map<int,room> room_listWink to create a class variable called room_list, or use room_list to create a class variable (room_list list_of_roomsWink called list_of_rooms or something similar.

In both cases, you should really use C++'s case sensitivity for naming variables. So for your typedef you'd have RoomList instead of room_list, and for b_id you'd have bId. Your function naming looks fine though. Just a thought Smile






Re: Visual C++ Language Using/Creating a MAP variable as a Class member

SWL8

Dear Iain, thanks for your help! that worked. And I changed my variable namings as well.



Re: Visual C++ Language Using/Creating a MAP variable as a Class member

Simple Samples

Shakje wrote:

Either don't use typedef there (std::map<int,room> room_list to create a class variable called room_list, or use room_list to create a class variable (room_list list_of_rooms called list_of_rooms or something similar.

Use of typedef in that manner would be the recomentation of C++ experts instead of use of std::map directly without a typedef, right I think it helps to emphasize the better solution.

Shakje wrote:

In both cases, you should really use C++'s case sensitivity for naming variables. So for your typedef you'd have RoomList instead of room_list, and for b_id you'd have bId. Your function naming looks fine though. Just a thought

Use of all lower case as in room_list is more consistent with Microsoft examples and the names used for C++ standard classes. It is a matter of personal preference and such. I prefer names such as RoomList but my career began as a maintenance programmer where I must be flexible; maintenance programmers cannot impose their preferences on everyone.