Reputation: 25
I'm currently putting together what I've learned of C++ by writing a small text adventure - just trying to get the basic class interrelations down so I can have the player move through a few rooms at the moment. I'm getting an 'expected unqualified id before '}' error in my room.h file when I compile. I think it may have something to do with a Room class member which is a vector of Exit object pointers, but I'm not sure. I'd appreciate a quick scan of the code just to let me know If I'm missing something obvious but important. Sorry if this gets complicated. I'll try to be brief and to the point.
I'm not sure what you all may need to see (codewise) and I don't want to throw up the whole code so...Let me outline how I have things set up, to start off with.:
1) I have a cpp file, called from main(), which instantiates 21 new rooms on the heap 2) Followed by another cpp file which instantiates new Exit objects on the heap, pushes them onto a vector, and calls a Room.set() function to pass the vector of Exit pointers to the Room class as one of its data members. Each exit in The vector will also have a pointer to one of the new Rooms created on the Heap.
The file to instantiate new rooms looks like this:
#include "RoomsInit.h"
#include "Room.h"
void InstantiateRooms()
{
string roomName1 = "On a deserted beach";
string roomDescr1 = "You are standing on a deserted beach. To the east, a "
"crystal blue ocean\n dances in the morning sun. To the "
"west is a dense jungle, and somewhere\n far off, you can "
"hear the singing of a strange bird. The white, sandy \n"
"beach runs out of sight to the north and south.\n\n\n";
Room* p_deserted_beach = new Room(roomName1, roomDescr1);
* Only the roomName and roomDescr is passed to the constructor at this point...and there are 20 more rooms like this in the file.
The Exit instantiate file looks like this:
#include "exitsInit.h"
#include "exit.h"
#include "room.h"
#include "RoomsInit.h"
void InstantiateExits()
{
vector<Exit*> exitVec;
Exit* p_north1 = new Exit("north", p_on_the_beach_north, true, false);
Exit* p_south1 = new Exit("south", p_on_the_beach_south1, true, false);
Exit* p_east1 = new Exit("east", p_in_the_ocean, true, false);
Exit* p_west1 = new Exit("west", p_in_the_jungle, true, false);
exitVec.push_back(p_north1);
exitVec.push_back(p_south1);
exitVec.push_back(p_east1);
exitVec.push_back(p_west1);
(*p_deserted_beach).SetExitVec(exitVec);
exitVec.clear();
The exitVec is created and sent to the Room class via the set function to become one of it's data members...There are 20 more sets of these in this file)one for each room).
My Room class header file, where I'm getting the compiler error, at the moment, looks like this:
#ifndef ROOM_H_INCLUDED
#define ROOM_H_INCLUDED
#include <iostream>
#include <string>
#include <vector>
class Exit;
using namespace std;
class Room
{
private:
string m_roomName;
string m_roomDescr;
string m_specDescr;
bool m_isSpecDescr;
vector<Exit*> m_exitVec;
public:
Room(string roomName, string roomDescr, string specDescr = "",
bool isSpecDescr = false);
string GetRoomName(); const
string GetRoomDes(); const
bool GetRoomSpecBool(); const
string GetRoomSpec(); const
void SetExitVec(vector<Exit*> exitVec);
vector<Exit*> GetExitVec(); const
};
#endif // ROOM_H_INCLUDED
----------- with the corresponding cpp file: --------------
#include "room.h"
Room::Room(string roomName, string roomDescr,
string specDescr, bool isSpecDescr) :
m_roomName(roomName), m_roomDescr(roomDescr),
m_specDescr(specDescr), m_isSpecDescr(isSpecDescr) {}
string Room::GetRoomName() const
{
return m_roomName;
}
string Room::GetRoomDes() const
{
return m_roomDescr;
}
bool Room::GetRoomSpecBool() const
{
return m_isSpecDescr;
}
string Room::GetRoomSpec() const
{
return m_specDescr;
}
void Room::SetExitVec(vector<Exit*> exitVec)
{
m_exitVec = exitVec;
}
vector<Exit*> Room::GetExitVec() const
{
return m_exitVec;
}
---------The Exit class header is this:
#ifndef EXIT_H_INCLUDED
#define EXIT_H_INCLUDED
#include <iostream>
#include <string>
#include <vector>
class Room; // For using a class pointer as a data member
using namespace std;
class Exit
{
private:
string m_exitName; // east, west, etc
Room* mp_exitTo;
bool m_isExit;
bool m_isExitHidden;
bool m_isExitPhrase;
string m_exitPhrase;
public:
Exit();
Exit(string exitName, Room* pExit, bool isExit, bool isExitHidden,
bool isExitPhrase = false, string exitPhrase = "");
string GetExitName(); const
Room* GetExitTo(); const
void SetIsExitTrue();
void SetIsExitFalse();
bool GetIsExit(); const
void SetIsExitHiddenTrue();
void SetIsExitHiddenFalse();
bool GetIsExitHidden(); const
bool GetIsExitPhrase(); const
string GetExitPhrase(); const
};
#endif // EXIT_H_INCLUDED
-------------and its cpp file:
#include "room.h"
#include "exit.h"
#include "RoomsInit.h"
Exit::Exit() :
mp_exitTo(NULL), m_isExit(false), m_isExitHidden(false) {}
Exit::Exit(string exitName, Room* pExit, bool isExit, bool isExitHidden,
bool isExitPhrase, string exitPhrase) :
m_exitName(exitName), mp_exitTo(pExit), m_isExit(isExit),
m_isExitHidden(isExitHidden), m_isExitPhrase(isExitPhrase),
m_exitPhrase(exitPhrase) {}
string Exit::GetExitName() const
{
return m_exitName;
}
Room* Exit::GetExitTo() const
{
return mp_exitTo;
}
void Exit::SetIsExitTrue()
{
m_isExit = true;
}
void Exit::SetIsExitFalse()
{
m_isExit = false;
}
bool Exit::GetIsExit() const
{
return m_isExit;
}
void Exit::SetIsExitHiddenTrue();
{
m_isExitHidden = true;
}
void Exit::SetIsExitHiddenFalse();
{
m_isExitHidden = false;
}
bool Exit::GetIsExitHidden() const
{
return m_isExitHidden;
}
bool Exit::GetIsExitPhrase(); const
{
return m_isExitPhrase;
}
string Exit::GetExitPhrase() const
{
return m_exitPhrase;
}
I'm also getting 21 warnings stating that the rooms I've created on the Heap are unused variables - not sure what that means. I feel like I'm missing something about the #includes relationships that is crucial, but I just can't see what it is...I've only been programming for about 8 months and most of the examples I've come across in books or online are somewhat less complex than what I'm doing right now. And so, I'd really appreciate any advice or comments y'all who are more experienced might have. Thanks. - Mark
Upvotes: 0
Views: 376
Reputation: 87959
In room.h
string GetRoomName(); const
string GetRoomDes(); const
bool GetRoomSpecBool(); const
string GetRoomSpec(); const
void SetExitVec(vector<Exit*> exitVec);
vector<Exit*> GetExitVec(); const
should be
string GetRoomName() const;
string GetRoomDes() const;
bool GetRoomSpecBool() const;
string GetRoomSpec() const;
void SetExitVec(vector<Exit*> exitVec);
vector<Exit*> GetExitVec() const;
You got your semi-colons in the wrong place.
Upvotes: 1