初学者C ++,指针和继承错误

时间:2013-04-07 14:21:12

标签: c++ visual-c++

我是一名精通csharp的初学c ++程序员但是..

对于我的作业,我们必须创建一个基于文本的迷宫游戏,到目前为止我有一个头文件,其中包含方向声明和设置方向N,S,W,E的方法

在主要内容我有设置和设置方向可以进入哪个方向。下面是代码的示例我已经包含了每个语句中的一个来缩短长度,即只有南方获取和设置。

void Room::setS(Room* nextRoom)
{
south = nextRoom;
}

Room* Room::gS(){return south;}

Room* A = new Room("A");
Room* B = new Room("B");

A->setE(B);
A->setS(E);

Room* myPosistion = A;
string location = *myPosistion->gName();

while(location !="L")
{
    cout << "You are In Room "<< location << endl;
    }

在while循环中,我需要能够访问Room :: Methods,(Room :: gS()),所以

if(location->gS() == NULL
//do nothing
else 
{
  cout<<"1. South"<<endl;
}

问题是,我无法获取位置变量来访问Room ::方法,我知道我遗漏了一些非常基本但却无法抓住它的东西..

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

您需要使用指向对象的指针而不是包含名称

的字符串

尝试更多内容:

while(myPosistion->gName().compare("L") != 0 )
{
    cout << "You are In Room "<< myPosistion->gName() << endl;
    if( myPosistion->gS() != NULL )
    {
        cout << "1. South : "<< myPosistion->gS()->gName() << endl;
    }
    //TODO some processing here!
}

当您使用位置变量(string)时,您对对象的引用