2017-04-11 4 views
1

私のCSクラスのテキストベースのrpgを作成する必要があります。このプログラムでは、世界を生成するための特定のタグを持つファイルをユーザーが入力できるようにする必要があります。私はクラス 'room'の私のベクトル 'rooms_f'が関数 'generate_rooms'のメンバー変数にアクセスできないという問題があります。ここでベクトルクラスのメンバー変数へのアクセス

は、関連するコードです:

class room{ 

public: 

    int room_index = 0; 
    vector<int> exit_index; 
    vector<string> exit_direction; 
    vector<bool> exit_locked; 
    vector<int> gold; 
    vector<int> keys; 
    vector<int> potions; 
    vector<bool> have_weapon; 
    vector<int> weapon_index; 
    vector<bool> have_scroll; 
    vector<int> scroll_index; 
    vector<bool> have_monster; 
    vector<int> monster_index; 

}; 


int main() { 

    string file_name;   //stores the name of the input file 
    ifstream input_file;  //stores the input file 
    player character;   //stores your character data 
    vector<room> rooms;   //stores data for rooms 

    player(); 

    cout << "\n\n\n\n\nEnter adventure file name: "; 
    cin >> file_name; 

    input_file.open(file_name.c_str()); 

    if (input_file.fail()) { 
    cerr << "\n\nFailed to open input file. Terminating program"; 
    return EXIT_FAILURE; 
    } 

    cout << "\nEnter name of adventurer: "; 
    cin >> character.name; 

    cout << "\nAh, " << character.name << " is it?\nYou are about to embark on 
     a fantastical quest of magic\nand fantasy (or whatever you input). 
     Enjoy!"; 

    generate_rooms(input_file, rooms); 

    return EXIT_SUCCESS; 

} 

void generate_rooms (ifstream& input_file, vector<room>& rooms_f) { 

    string read; 
    int room_index = -1; 
    int direction_index = -1; 

    while (!input_file.eof()) { 

    room_index += 1; 

    input_file >> read; 

    if (read == "exit") { 

     direction_index += 1; 

     input_file >> read; 
     rooms_f.exit_index.push_back(read); 

     input_file >> read; 
     rooms_f.exit_direction.push_back(read); 

     input_file >> read; 
     rooms_f.exit_locked.push_back(read); 

    } 

    } 

} 

与えられたコンパイラエラーがある:

prog6.cc: In function ‘void generate_rooms(std::ifstream&, 
std::vector<room>&)’: 
prog6.cc:168:15: error: ‘class std::vector<room>’ has no member named 
‘exit_index’ 
     rooms_f.exit_index.push_back(read); 
      ^
prog6.cc:171:15: error: ‘class std::vector<room>’ has no member named 
‘exit_direction’ 
     rooms_f.exit_direction.push_back(read); 
      ^
prog6.cc:174:15: error: ‘class std::vector<room>’ has no member named 
‘exit_locked’ 
     rooms_f.exit_locked.push_back(read); 
      ^
+0

エラーと同様です。 'rooms_f'は' room'ではなく 'vector'ですので、' exit_index'というメンバはありません。一度に1つの 'room 'を作成して' rooms_f'に追加しますか? – aschepler

答えて

1

rooms_fは部屋のベクトルですが、あなたはその中の特定の部屋のフィールドにアクセスすることができます前に、あなたは部屋を選択する必要があります。あなたは、あなたのベクトルのインデックスでアイテムにアクセスするステップを逃しました。

vector<room>[index].field_name 

ルームオブジェクトを使用する前に、初期化する必要があります。

関連する問題