Confused About Member Access Train in Class? - c++

I have the following class:
#include <string>
#include <cstdlib>
using namespace std;
class Locker{
public:
int lockerId;
string renterName;
double monthlyRent;
// The variable is true when the locker is a vip locker
//if the locker is a regular locker then this variable is set to false
bool isVip;
bool isRentOverdue;
Locker(){};
Locker(int id, string name, double rent, bool vip=0, bool overdue=0);
bool operator==(Locker const &other);
};
EDIT: LOCKER NODE CLASS
class LockerNode{
public:
Locker objLocker;
LockerNode *next;
LockerNode(){
next=0;
};
LockerNode(Locker e, LockerNode *ptr=0){
objLocker=e;
next=ptr;
}
};
and implementation:
#include <sstream>
#include "Locker.h"
#include <iostream>
using namespace std;
Locker::Locker(int id, string name, double rent, bool vip, bool overdue){
lockerId=id; renterName=name; monthlyRent=rent; isVip=vip; isRentOverdue=overdue;
}
bool Locker::operator==(Locker const &other){
if(lockerId==other.lockerId && renterName==other.renterName && monthlyRent==other.monthlyRent && isVip==other.isVip && isRentOverdue==other.isRentOverdue)
return true;
else return false;
}
I have the following code in a function, trying to track both the number of objects in a linked list, and perform some operations on them based on how many their are and their properties. I am passing in e which is a newly created object. If an object has property vip = true, I need to place it ahead of other non-vip objects, unless there is already a vip object, in which case it goes just behind it. Hence, the following code:
int count = 0;
LockerNode *p = head;
for(;p!=0;count++, p=p->next) {
if(count == 1) {
if (e.isVip) {
if(p->isVip) // !!!!!!!!!Issue here!!!!!!!!!!
}
}
I checked the parameter fine to determine if it is vip or not. However, I am unsure how to check the current element I am at in the list for the same. My above effort on the line in question has not worked. I'm a little confused as to the syntax. Can anyone help me out?
Thanks!

where is your locker Node class? problem could be there...
ok try replacing this :
if(p->isVip) // !!!!!!!!!Issue here!!!!!!!!!!
with:
if (p->objLocker.isVip) {//true/false for this node
p is a pointer accesing his members is with->
but objlocker is not a pointer accesing his members is with a "."

Related

Pass created object as argument, from inside that object, C++

I am facing the following problem. I have the following classes, Room and Reservation . For Reservation class there is a function (void Reservation :: rsrvRoomAssignment(Room* r)) that assigns the Room* rsrvRoom member of Reservation class, to a Room object. I want though to call this class from inside the Room object but i have no clue on how to achieve that properly passing as argument the created object that runs the code.
The code describes the above:
Reservation.h
class Room;
class Reservation {
public:
static int rsrvCode;
string rsrvName;
unsigned int rsrvDate;
unsigned int rsrvNights;
unsigned int rsrvPersons;
Room* rsrvRoom; // Room assigned to reservation.
Reservation();
~Reservation();
void rsrvRoomAssignment(Room*);
};
Reservation.cpp
#include <iostream>
#include "Reservation.h"
using namespace std;
/*
Constructor & Destructor
*/
void Reservation :: rsrvRoomAssignment(Room* r){
rsrvRoom=r;
}
Room.h
#include "Reservation.h"
class Room {
public:
static unsigned int roomNumber;
unsigned int roomCapacity;
Reservation* roomAvailability[30];
double roomPrice;
Room();
~Room();
bool roomReservationAdd(Reservation*);
};
Room.cpp
#include <iostream>
#include <string>
#include "Room.h"
using namespace std;
/*
Constructor & destructor
*/
bool Room::roomReservationAdd(Reservation* r){
/* some statement that returns flase */
r->rsrvRoomAssignment(/* & of created object */); // This is the problem i described.
return 1;
}
I am pretty new to OOP so there might be some more logical errors on the above snipsets, so don't be harsh :) .
Thanks for any kind of help!
When inside a class method, this indicates the instance of the object calling it. In your case, when a room instance X calls X.roomReservationAdd(r),
this points to the room instance X.
Hence, you can simply call r->rsrvRoomAssignment(this);

Memory management for 2 "interconnected" C++ classes

I have a class Branch and a class Tree. The declaration of the classes looks like:
class Branch{
private:
map<string, double> properties;
vector<Branch *> parent;
vector<Branch *> children;
int hierarchy;
public:
...
}
class Tree{
private:
/* Tree is a vector of pointers to branches. */
vector<Branch*> tree_branches;
map<int,int> Hierarchy_distribution;
public:
...
}
1) If I understood well, the fact that the only attributes of Tree class are a vector and a map, makes it unnecessary to declare a destructor, a copy-assignement operator and a copy constructor, because the memory is managed "inside" the vector and map templates.
2) I use this classes from a python code (I used cython to interface between C++ an python) and all the operations I make are performed through a tree object. I thought that because the branches I "use" are contained in a tree object (with a good memory management) I didn't need to declare a destructor, a copy constructor,and a copy assignment operator for the branch class. However I'm experiencing some problems and I think I have a memory leak.
Can someone confirm me if this could be causing a memory leak? If it is would stocking the int hierarchy inside a vector<int> avoid declaring a destructor and company?
EDIT
The branches who are stocked in the tree are created inside a method of the Tree class. It looks like:
Tree::addBranch(){
Branch* branch2insert=new Branch();
tree_branches.push_back(branch2insert);
}
As a local variable, is branch2insert destructed at the end of the scope? Do I need to write delete branch2insert;? Does someone have an idea of where do the Branch object at which I point to lives?
I still don't get why I need to ensure memory management when I never allocate ressources except inside the Tree class methods...
This whole thing is getting very messy in my head
EDIT 2:EXAMPLE
branch.h
#ifndef BRANCH_H_
#define BRANCH_H_
#include <memory>
#include <string>
#include <vector>
#include <map>
using namespace std;
class Branch{
private:
vector<Branch*> parent;
vector<Branch*> children;
public:
Branch();
bool hasParent();
bool hasParent(Branch* test);
void addParent(Branch* p);
void removeParent();
Branch* getParent();
bool hasChildren();
bool hasChild(Branch*test);
void addChild(Branch*ch);
void removeChild(Branch*ch);
void removeChildren();
void removeDescendants();
vector<Branch*> getBrothers();
};
#endif
tree.h
#ifndef TREE_H_
#define TREE_H_
#include <vector>
#include <map>
#include"branch.h"
using namespace std;
class Tree{
private:
vector<Branch*> tree_branches;
public:
Tree();
int getNumberOfBranches();
Branch* getBranch(int branch_index); /* Returns branch at index. */
int getIndex(Branch* branch); /* Returns the index of branch. */
int getLastDescendantIndex(int ancestor_index); /* Returns index of the last descendant of branch at ancestor index. */
int getParentIndex(int child_index); /* Returns index of the parent of branch at child_index. */
vector<int> getBrothersIndex(int branch_index); /* Returns indexes of the brothers of branch at branch_index. */
void addBranch(int parent_index); /* Adds branch without initializing its properties. */
void removeBranch(int branch_index); /* Removes branch at branch_index and all its descendants. */
};
#endif
branch.cpp
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <sstream>
#include <map>
#include <stdio.h>
using namespace std;
#include "branch.h"
Branch::Branch():parent(vector<Branch*>()),children(vector<Branch*>())
{
}
bool Branch::hasParent(){
if(parent.size()==0)
return false;
else
return true;
}
bool Branch::hasParent(Branch* test){
bool ret = false;
for(vector<Branch*>::iterator it=parent.begin();it!=parent.end();it++){//traversing parent vector
if((*it)==test){//if test belong to parent vector
ret = true;
break;
}
}
return ret;
}
void Branch::addParent(Branch* mom){
if(parent.size()==0){//if a branch hasn't a parent, multiple parents aren't allowed in a tree-network
if(hasParent(mom)==false){//double checking if mom isn't already a parent
parent.push_back(mom);//adding mom to parent vector
}
else{
cout << "Branch::addParent Error: trying to add a parent twice.\n";
}
}
else{
cout << "Branch::addParent Error: trying to add a parent to a branch that already has one.\n";
}
}
void Branch::removeParent(){
if(this->hasParent()==true){//if this branch has a parent
vector<Branch*>::iterator it=parent.begin();
parent.erase(it);//erase it (it is the first and only element of the vector)
}
else{
cout << "Removing the trunk.\n";
}
}
Branch* Branch::getParent(){
return parent[0];
}
bool Branch::hasChildren(){
if(children.size()==0)
return false;
else
return true;
}
bool Branch::hasChild(Branch* test){
bool ret = false;
for(vector<Branch*>::iterator it=children.begin();it!=children.end();it++){
if((*it)==test){
ret = true;
break;
}
}
return ret;
}
void Branch::addChild(Branch* ch){
if(hasChild(ch)==false){
children.push_back(ch);
ch->addParent(this); // PARENTHOOD LINK ESTABLISHED IN ADD CHILD. ONLY
// NEEDED ONCE.
}
else{
cout << "Branch::addChild Error: trying to add a child but the child has been already added.\n";
}
}
void Branch::removeChild(Branch* ch){
if(hasChild(ch)==true){
for(vector<Branch*>::iterator it=children.begin();it!=children.end();it++){
if((*it)==ch){
children.erase(it);
break;
}
}
}
else{
cout << "Branch::removeChild Error: trying to remove a child that doesn't exist.\n";
}
}
void Branch::removeChildren(){
if(this->hasChildren()==true){
children.erase(children.begin(),children.end());
}
else{
cout << "Branch::removeChildren Error: trying to remove all the children of a branch but tha branch hasn't any.\n";
}
}
void Branch::removeDescendants(){
if (this!=NULL){
if(this->hasChildren()==true){
for(vector<Branch*>::iterator it=children.begin();it!=children.end();it++){
(*it)->removeDescendants();
}
removeChildren();
}
for(vector<Branch*>::iterator it=children.begin();it!=children.end();it++){
(*it)->removeParent();
}
}
}
vector<Branch*> Branch::getBrothers(){
vector<Branch*> brothers;
vector<Branch*> potential_brothers;
if (parent.size()!=0){
potential_brothers=parent[0]->children;
for (vector<Branch*>::iterator it=potential_brothers.begin();it!=potential_brothers.end();it++){
if ((*it)!=this){
brothers.push_back((*it));
}
}
}
return brothers;
}
tree.cpp
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
#include "tree.h"
Tree::Tree():tree_branches(vector<Branch*>()){
Branch* trunk=new Branch();
tree_branches.push_back(trunk);
}
int Tree::getNumberOfBranches(){
int number_of_branches=tree_branches.size(); //retrieving size of vector
return number_of_branches;
}
Branch* Tree::getBranch(int index){
unsigned int u_index=index;
return tree_branches.at(u_index); // returning branch at index of the tree vector
}
int Tree::getIndex(Branch* branch){
int index=0;
for(vector<Branch*>::iterator it=tree_branches.begin();it!=tree_branches.end();it++){
if((*it)==branch){
return index;
}
index++;
}
cout << "Tree::getIndex Error: trying to get the index of a branch we can't find. Returning 0.\n";
return 0;
}
int Tree::getLastDescendantIndex(int ancestor_index){
if(tree_branches.at(ancestor_index)->hasChildren()==false){// if it is a leaf
return ancestor_index;
}
if(ancestor_index==0){// if it is the trunk
int N=tree_branches.size();
int last_descendant_index=N-1;
return last_descendant_index;
}
vector<int> brothers_indexes=Tree::getBrothersIndex(ancestor_index);
for(vector<int>::iterator it=brothers_indexes.begin();it!=brothers_indexes.end();it++){
int brother_index=(*it);
if(brother_index>ancestor_index){
int last_descendant_index=brother_index-1;
cout << "The last descendant of" << ancestor_index << " is "<<last_descendant_index<<"\n";
return last_descendant_index;
}
}
int parent_index=Tree::getParentIndex(ancestor_index);
Tree::getLastDescendantIndex(parent_index);
}
int Tree::getParentIndex(int child_index){
if(child_index==0){ //if considered branch is the trunk
cout << "Tree::getParentIndex: the trunk hasn't a parent. Returning -1.\n";
return -1;
}
unsigned int u_child_index=child_index;
Branch* parent=tree_branches.at(u_child_index)->getParent(); //retrieving the parent of the considered branch
int parent_index=Tree::getIndex(parent);
return parent_index; //returning parent index
}
vector<int> Tree::getBrothersIndex(int branch_index){
vector<int> brothers_index;
Branch* this_branch=Tree::getBranch(branch_index);//retrieving the branch from the index
vector<Branch*> brothers=this_branch->getBrothers();//retrieving branch's brothers
for(vector<Branch*>::iterator it=brothers.begin();it!=brothers.end();it++){ //traversing a vector containing the brothers of the consideered branch
int this_index=Tree::getIndex(*it); //retrieving index of a brother
brothers_index.push_back(this_index); //stocking the index in a vector
}
return brothers_index; //returning the vector containing the index of all brothers
}
void Tree::addBranch(int parent_index){
unsigned int u_parent_index=parent_index;
Branch* mom=tree_branches.at(u_parent_index);//getting futur parent
Branch* branch2insert=new Branch();//creation of branch to insert
mom->addChild(branch2insert);//setting family relationship
vector<Branch*>::iterator begin=tree_branches.begin();//creating iterators to manipulate vector elements
unsigned int inserting_position=u_parent_index+1;//initializing inserting_position
tree_branches.insert(begin+inserting_position,branch2insert);//inserting new branch
}
void Tree::removeBranch(int branch_index){
int N=tree_branches.size();
unsigned int u_branch_index=branch_index;
Branch* branch2remove=tree_branches.at(u_branch_index);
branch2remove->removeParent(); //removing parenthood link, if branch2remove is the trunk nothing will be done
if(branch_index!=0){//removing childhood link between parent and branch_index
Branch* branch2removeParent=branch2remove->getParent();
branch2removeParent->removeChild(branch2remove);
}
int last_descendant_index=Tree::getLastDescendantIndex(branch_index);
cout<<"The branch to remove is "<<branch_index<<", its last descendant index is "<<last_descendant_index<<", the size of the tree is "<<N<<".\n";
branch2remove->removeDescendants();//removing family links for all descendents
if(last_descendant_index==N-1){
vector<Branch*>::iterator begin=tree_branches.begin();
tree_branches.erase(tree_branches.begin()+u_branch_index,tree_branches.end());
}
else{
vector<Branch*>::iterator begin=tree_branches.begin();
unsigned int u_last_descendant_index=last_descendant_index;
tree_branches.erase(tree_branches.begin()+u_branch_index,tree_branches.begin()+u_last_descendant_index+1);//removing the considered branch and its descendents from the tree vector
}
}
test.cpp
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <typeinfo>
#include <vector>
#include <map>
#include <string>
#include "tree.h"
using namespace std;
int main(){
Tree tree;
tree=Tree();
for(int i=0;i<5;i++){
int N=tree.getNumberOfBranches();
for(int j=0;j<N;j++){
tree.addBranch(j);
tree.addBranch(j);
}
}
tree.removeBranch(21);
tree.removeBranch(30);
tree.removeBranch(50);
return 0;
}
to compile:
g++ -Wall -pedantic -c
1) If I understood well, the fact that the only attributes of Tree class are a vector and a map, makes it unnecessary to declare a destructor, a copy-assignement operator and a copy constructor, because the memory is managed "inside" the vector and map templates.
You are wrong here, as the memory which is managed inside the vector is only the storage for the addresses of branches(Branch *). When you insert some branch you call something like tree_branches.push_back(new Branch), don't you? Thus, you will need to call the corresponding delete somehow, otherwise the default destructor of the tree won't take care of calling the destructors of the Branches.
A quick fix for your code could be switching from Branch * to shared_ptr<Branch>. In such way, when the tree_branches vector is destroyed, the shared_ptr's will also be destroyed and when the latest shared_ptr to any Branchis destroyed, also the pointed Branch will be.
would stocking the int hierarchy inside a vector avoid declaring a destructor and company?
It would change jack squat, as the Branches default destructors still would not be called, and so the destructors of the Branches members would never be called.
Finnally there isn't a memory leak. Thanks #Yunnosch for mentionning Valgrind, I wasn't aware this kind of tool existed.
#Davide I accepted your answer since it provided an alternative to what I've done, even if apparently there aren't problems with the management of the memory in my code.
The code I posted has a problem in function Tree::getLastDescendantIndex.

C++ Why didn't the default copy work? [duplicate]

This question already has answers here:
Why does the C++ map type argument require an empty constructor when using []?
(6 answers)
Closed 5 years ago.
I've done a lot of Googling and can't seem to figure out what's going on. I'm teaching myself C++ (I'm more familiar with Java).
I have Item Class objects that are being stored in an Inventory Class map, not as pointers. I want to retrieve one of the items from the Inventory in a function, assign it to a temp variable while I delete it from the Inventory map, and then return the object itself so something else can use it. When I originally tried using the code within my function it was returning the error (followed by the stack trace of c++ library stuff):
no matching constructor for initialization of 'Item'
::new ((void*)__p) _Tp();
I tried creating a copy constructor, but to no avail. Eventually, it worked by including an empty constructor ( Item(); ) in my header file and defining it in my cpp file ( Item::Item() {} ).
I would just like to understand why this was necessary so I can recognize it in the future to know what I'm doing.
EDIT: Upon further inspection of the error stack trace, it turned out the actual problem with with the Inventory::addItem function. When assigning an object to a map using operator[], the map first instantiates the value type to the key using the default constructor before making the assignment. No default constructor was available, so the error was returned.
It was fixed by changing the line to map.insert({key, value})
Here are the important parts of the two class files:
//item.h
#include <string>
using namespace std;
class Item {
private:
string name;
int type;
int levelReq;
public:
Item(string name, int type, int levelReq);
Item();
string getName() {return name;}
int getType() {return type;}
friend ostream &operator<<(ostream &out, const Item &item);
};
---------------------------------------------------------------
//item.cpp
#include <string>
#include "item.h"
using namespace std;
Item::Item(string n, int t, int l) : name(n), type(t), levelReq(l) {}
Item::Item() {}
ostream &operator<<(ostream &out, const Item &item) {
return out << item.name;
}
---------------------------------------------------------------
//inventory.h
#include <map>
#include "item.h"
class Inventory {
private:
map <int, Item> inventory;
int size;
bool full;
int nextFree;
void findNextFree();
public:
Inventory();
bool isFull() {return full;}
void addItem(Item item);
Item getItem(int slot);
void showInv();
};
---------------------------------------------------------------
//inventory.cpp
#include <iostream>
#include <string>
#include "inventory.h"
#include "item.h"
using namespace std;
Inventory::Inventory() {
full = false;
nextFree = 1;
size = 28;
}
void Inventory::addItem(Item item) {
if (!full) {
inventory[nextFree] = item;
findNextFree();
}
else {
cout << "Your inventory is full (Inv::addItem)";
}
}
Item Inventory::getItem(int slot) {
Item item = inventory.at(slot);
inventory.erase(slot);
full = false;
if (nextFree > slot) {
nextFree = slot;
}
return item;
}
void Inventory::findNextFree() {
nextFree++;
if (nextFree == size + 1) {
full = true;
}
else if (inventory.count(nextFree)) {
findNextFree();
}
}
I think the issue rose because you declared a constructor for your item class.
C++ will automatically generate the necessary constructors if you don't provide any custom constructors.
The necessary constructors are the default, copy and move constructors.
The moment you provide one, the default constructors won't be generated and you have this issue. This principle will also apply to structs.
Check the reference to see for yourself:
http://en.cppreference.com/w/cpp/language/default_constructor
http://en.cppreference.com/w/cpp/language/copy_constructor
http://en.cppreference.com/w/cpp/language/move_constructor
Hope this answers your question.

Access struct members inside a class private members?

For Example:
graph.h
#ifndef GRAPH_H
#define GRAPH_H
#include <iostream>
#include <string>
using namespace std;
class graph
{
private:
struct node
{
string name;
int currentValue;
struct node *next;
};
node* head;
public:
graph();
~graph();
graph(string* newName, int* givenValue);
}
#endif
graph.cpp
#include "graph.h"
graph::graph() {}
graph::~graph() {}
graph::graph(string* newName, int* givenValue)
{
//This is what I want to achieve
this->node->name = newName; //Compile error
}
main.cpp
#include "graph.h"
#include <iostream>
using namespace std;
int main()
{
return 0; //Note I have not yet created a graph in main
}
How can I access the struct node members for the function above?
This is the error:
graph.cpp: In constructor ‘graph::graph(std::string*, double*)’:
graph.cpp:24:8: error: invalid use of ‘struct graph::node’
this->node->label = newName;
The problem has nothing to do with your private struct. The constructor should be able to access all private members.
The problem as that you confused the struct name node and the variable name head:
this->node->name = newName; // incorrect
Instead you should write:
this->head->name = *newName;
If you want to access the class variable you should call
this->head->name = *newName;
though you can omit this-> so the following is fine
head->name = *newName;
Couple of other notes:
string* newName is a pointer so you need to access its value with the dereference operator "*" (i.e. head->name = *newName; instead of head->name = newName;
node* head is a pointer and currently you are trying to access an uninitialized pointer. You probably need something like head = new node(); as well.
Your problem is not related to private access. First, add ; to end your class declaration:
class graph
{
// ...
};
Then, you typed this->node->name while nodeis a type. Change this line for this->head->name. Note that the pointer head is uninitialized here.
And then, newName is of type string* while this->head->name is of type string. Depending on how you want to use your class, you may consider modifying your code like this:
graph::graph(const string& newName, int givenValue):
head(new node)
{
//This is what I want to achieve
this->head->name = newName;
}
Or like this:
graph::graph(string* newName, int* givenValue):
head(new node)
{
//This is what I want to achieve
this->head->name = *newName;
}
Also, read about rule of 3/5/0.

class objects as data members and header file includes

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
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.