类对象作为数据成员和头文件包含

时间:2013-09-28 23:13:59

标签: c++

我目前正在通过编写一个小文本冒险来整理我所学到的C ++ - 只是试图让基本的课程相互关联,这样我就可以让玩家此刻移动几个房间。当我编译时,我的room.h文件中出现'预期的不合格ID'}'错误。我认为它可能与Room类成员有关,它是Exit对象指针的向量,但我不确定。我很欣赏快速扫描代码只是为了让我知道如果我错过了一些明显但重要的东西。对不起,如果这变得复杂。我会尽量简明扼要。

我不确定你们所有人可能需要看到什么(代码方面)而且我不想抛出整个代码所以......让我概述一下如何设置,开始时:

1)我有一个cpp文件,从main()调用,它在堆上实例化21个新房间 2)跟随另一个cpp文件,该文件在堆上实例化新的Exit对象,将它们推送到向量上,并调用Room.set()函数将Exit指针的向量作为其数据成员之一传递给Room类。向量中的每个出口也将有一个指向堆上创建的新房间之一的指针。

实例化新房间的文件如下所示:

#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);

* 此时只有roomName和roomDescr传递给构造函数......文件中还有20个这样的房间。

Exit instantiate文件如下所示:

#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();

创建exitVec并通过set函数发送到Room类,成为其中一个数据成员......此文件中还有20多个这样的集合)每个房间一个)。

My Room类头文件,我现在收到编译器错误,如下所示:

#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;
}

--------- Exit类头是这样的:

#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

-------------及其cpp文件:

#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;
}

我还得到21个警告,说明我在堆上创建的房间是未使用的变量 - 不确定这意味着什么。我觉得我错过了一些关键的#includes关系,但我只是看不出它是什么......我只编程了大约8个月,我遇到的大多数例子都是书本或网上的内容比我现在做的要复杂一些。因此,我非常感谢任何有经验的人可能提出的建议或意见。谢谢。 - 马克

1 个答案:

答案 0 :(得分:1)

在room.h中

   string GetRoomName(); const
    string GetRoomDes(); const
    bool GetRoomSpecBool(); const
    string GetRoomSpec(); const

    void SetExitVec(vector<Exit*> exitVec);
    vector<Exit*> GetExitVec(); const

应该是

   string GetRoomName() const;
    string GetRoomDes() const;
    bool GetRoomSpecBool() const;
    string GetRoomSpec() const;

    void SetExitVec(vector<Exit*> exitVec);
    vector<Exit*> GetExitVec() const;

你的分号在错误的地方。