setRoomName不会更改变量roomName

时间:2013-05-09 18:40:11

标签: c++ setter

我从文本文件中取出房间并尝试使用变量中的信息,所以在我将信息添加到链接列表后,按房间ID搜索我想要的房间,然后使用setRoomName =到了房间,它找到了名字。但这不起作用......任何人都可以看到问题吗?

这是我的代码:

#include "Rooms.h"
#include "DoublyLinkedList.h"
#include <string>
using namespace std;

DoublyLinkedList<Rooms> roomsList;
DoublyLinkedListIterator<Rooms> itr = roomsList.getIterator();

 Rooms :: Rooms()
{
    this->roomID = 0;
    this->roomName = "";
    this->roomExits = "";
}

 Rooms :: Rooms(int roomID,string roomName,string roomExits)
 {
     this->roomID = roomID;
     this->roomName = roomName;
     this->roomExits = roomExits;
 }
    int Rooms :: getRoomID()
    {
        return roomID;
    }
    void Rooms :: setRoomID(int roomID)
    {
        this->roomID = roomID;
    }
    string Rooms :: getRoomName()
    {
    return roomName;
    }
    void Rooms ::setRoomName(string roomName)
    {
        this->roomName = roomName;
    }
    string Rooms :: getRoomExits()
    {
    return roomExits;
    }
    void Rooms :: setRoomExits(string roomExits)
    {
        this->roomExits = roomExits;
    }
 void Rooms :: loadRooms()
 {
    string fileName = "Rooms\\Rooms.txt";
    ifstream infile(fileName);
    string garbage;
    int loadRoomID;
    string loadRoomName1;
    string loadRoomName2;
    string loadRoomExits;
    while(infile >>garbage >> garbage >> loadRoomID  >> garbage >>
          garbage >> garbage >> loadRoomName1 >> loadRoomName2 >> garbage 
          >> garbage >> loadRoomExits)
    {

        cout << "Room ID: \t\t"<< loadRoomID << "\n";
        cout << "Room Name: \t\t"<< loadRoomName1 <<" " << loadRoomName2 << "\n";
        cout << "Room Exits: \t\t" << loadRoomExits <<"\n";
        string loadRoomName = loadRoomName1 + loadRoomName2;
        Rooms r1 (loadRoomID,loadRoomName,loadRoomExits);
        roomsList.Append(r1);
    }

 }
 void Rooms :: printRooms()
    {
        int index = 0; 
        //Loop through the iterator.
        for(itr.Start();itr.Valid();itr.Forth())
        {
            index++;
            cout << "------------------Rooms------------------\n";
            cout << "--------------------------------\n";
            cout << "Position:\t\t" << index << "\n";
            cout << "--------------------------------\n";
            cout << "Room ID:\t\t" << itr.Item().getRoomID() << "\n"; 
            cout << "Room Name:\t\t" << itr.Item().getRoomName() << "\n"; 
            cout << "Room Exits:\t\t" << itr.Item().getRoomExits() << "\n";
            cout << "------------------------------------------\n";
        }
        cout << "Rooms: \t\t" << roomsList.getCount() << "\n";
    }
    string Rooms :: searchByRoomID(int searchByID)
{
    //Loop through the Iterator.
    for (itr.Start(); itr.Valid(); itr.Forth())
        {
            //If the object entered has the same first name as the one in the loop.
            if (itr.Item().getRoomID() == searchByID)
            {
                //Print out the details from the list.
                cout << "------------------------------------------\n"; 
                cout << "Room ID:\t\t" << itr.Item().getRoomID() << "\n"; 
                cout << "Room Name:\t\t" << itr.Item().getRoomName() << "\n"; 
                cout << "Room Exits:\t\t" << itr.Item().getRoomExits() << "\n";
                cout << "------------------------------------------\n";
                setRoomID(itr.Item().getRoomID());
                setRoomName(itr.Item().getRoomName());
                setRoomExits(itr.Item().getRoomExits());
                cout << getRoomID();
                cout << getRoomName();
                cout << getRoomExits();
            }
        }
    return getRoomName();
}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我发现的错误是你找到一个房间通过id然后你设置id,name,exit就像你找到的一样。那你怎么能找到一个改变

setRoomID(itr.Item().getRoomID());
setRoomName(itr.Item().getRoomName());
setRoomExits(itr.Item().getRoomExits());

而不是这个,你应该给出一些你需要为Exmple

设置的其他值
setRoomID(789); 
setRoomName("Room ");
setRoomExits("yes");

此处找到的房间的ID,名称,出口分别设置为789,“房间”,“是”