I am trying to create a list of rooms to add an location number. But i am getting errors. Why am i getting those errors?
code:
1 2 3 4 5 6 7 8 9 10
list<Room> room;
list<Room>::iterator pcit;
int i = 0; //help to add a diiferent number for each room
for(list<Room>::iterator pcit = room.begin(); pcit !=room.end(); ++pcit)
{
room.push_back(); // add a room
int num = 1000 +i; // first location will be 1000, then 1001 etc...
room[pcit].setlocation(num); // set the location number of each room
++i;
}
errors:
F:\CodeBlocks-Programs\QuickRPG\main.cpp||In function 'int main()':|
F:\CodeBlocks-Programs\QuickRPG\main.cpp|303|error: no matching function for call to 'std::list<Room, std::allocator<Room> >::push_back()'|
f:\programs\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_list.h|919|note: candidates are: void std::list<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = Room, _Alloc = std::allocator<Room>]|
F:\CodeBlocks-Programs\QuickRPG\main.cpp|305|error: no match for 'operator[]' in 'room[pcit]'|
||=== Build finished: 2 errors, 0 warnings ===|
but how would I have to set it up to work right? I have no idea what the error means. I tried this:
1 2 3 4 5 6 7 8 9
list<Room> room;
list<Room>::iterator pcit;
int i = 0;
for(list<Room>::iterator pcit = room.begin(); pcit !=room.end(); ++pcit)
{
int num = 1000 +i;
room.push_back(room.setlocation(num));
++i;
}
and got this error:
F:\CodeBlocks-Programs\QuickRPG\main.cpp|304|error: 'class std::list<Room, std::allocator<Room> >' has no member named 'setlocation'|
EDIT:
coder777,
Oh my bleep! I tried it and it worked! Thanks. This is what i did:
1 2 3 4 5 6 7 8 9 10 11 12
list<Room> room;
list<Room>::iterator pcit;
int i = 0;
for(list<Room>::iterator pcit = room.begin(); pcit !=room.end(); ++pcit)
{
int num = 1000 +i;
room.push_back(*pcit);
pcit->setlocation(num);
room.back().setlocation(num);
//room.push_back(room.setlocation(num));
++i;
}
Is that what you meant? I think I understand the use. But I will have to re-read the <list> article and the code I'm now using. Thanks again, coder777. :)
I think I messed something up. I want to setup doors and where they lead to. but I amhaing trouble accessing my rooms member functions to do so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int main()
{
list<Room> room;
list<Room>::iterator pcit;
int i = 0;
for(list<Room>::iterator pcit = room.begin(); pcit !=room.end(); ++pcit)
{
int num = 1000 +i;
room.push_back(*pcit);
pcit->setlocation(num);
room.back().setlocation(num);
++i;
}
Room[0]->setdoore(true); // sets east door to exist
Room[0]->setide(1002); // sets east door id to go to room 1002
What am I not understanding about lists that I can't do it?
Are you sure that you want to take an element of the list and push back to that list? It will cause an infinite loop since there will always be an element to push back.
I guess what you want is this:
1 2 3 4 5 6 7 8 9 10 11 12
list<Room> room;
list<Room>::iterator pcit;
int i = 0;
for(list<Room>::iterator pcit = room.begin(); pcit !=room.end(); ++pcit)
{
int num = 1000 +i;
// This will cause an infinite loop: room.push_back(*pcit);
pcit->setlocation(num);
// Without push_back you don't need this: room.back().setlocation(num);
//room.push_back(room.setlocation(num));
++i;
}
EDIT: You can do that without iterator:
1 2 3 4
for(list<Room>::size_type i = 0; i < room.size(); ++i)
{
pcit->setlocation(1000 +i);
}
I guess I am trying to setup a number of rooms(say 10 of them). And then I will go back and manually setup the doors and what-not to each room.
Intended program progression:
1-> make several rooms which are pre-set with an identifying number
2-> set doors to exist in each room, manually
3-> set where each door of each room leads to when it is used
I know it will be simpler(or so I thought) to use <list> and a for loop to create the rooms and identify them. I know(I think) I will have to set each rooms other characteristics myself. I didn't even want to try to create a list of rooms and characteristics in a text file. Not enough Tylenol in the house for that.
int main()
{
list<Room> room;//an empty list
list<Room>::iterator pcit;
int i = 0;
for(list<Room>::iterator pcit = room.begin(); pcit !=room.end(); ++pcit) //never executes (if it does, infinite loop because the list is growing)
{
int num = 1000 +i;
room.push_back(*pcit); //copying rooms
pcit->setlocation(num);
room.back().setlocation(num);
++i;
}
Room[0]->setdoore(true); // Room is a class, not an object
Room[0]->setide(1002);
I am trying to create a list of rooms to add an location number