C++ 访问向量类中的成员变量
我必须为我的CS类制作一个基于文本的rpg。在这个程序中,我必须允许用户输入一个有特定标签的文件来生成一个世界。我遇到一个问题,类“room”的向量“rooms\u f”无法访问函数“generate\u rooms”中的成员变量 以下是相关代码:C++ 访问向量类中的成员变量,c++,C++,我必须为我的CS类制作一个基于文本的rpg。在这个程序中,我必须允许用户输入一个有特定标签的文件来生成一个世界。我遇到一个问题,类“room”的向量“rooms\u f”无法访问函数“generate\u rooms”中的成员变量 以下是相关代码: class room{ public: int room_index = 0; vector<int> exit_index; vector<string> exit_direction; vector&l
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);
}
}
}
教室{
公众:
int room_index=0;
向量退出指数;
矢量出口方向;
向量退出锁定;
矢量金;
向量键;
媒介药剂;
矢量武器;
向量u指数;
矢量有卷轴;
向量卷轴索引;
矢量有_怪物;
向量指数;
};
int main(){
string file_name;//存储输入文件的名称
ifstream input_file;//存储输入文件
player character;//存储您的角色数据
向量房间;//存储房间的数据
player();
cout>文件名;
input_file.open(file_name.c_str());
if(input_file.fail()){
cerr字符名;
不能阅读;
房间退出索引,向后推(读取);
输入文件>>读取;
房间f.退出方向。向后推(读取);
输入文件>>读取;
房间出口锁定。推回(读取);
}
}
}
给出的编译器错误为:
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);
^
prog6.cc:在函数“void generate_rooms”(std::ifstream&、,
std::vector&)':
prog6.cc:168:15:错误:“class std::vector”没有名为
“退出索引”
房间退出索引,向后推(读取);
^
prog6.cc:171:15:错误:“class std::vector”没有名为
“退出方向”
房间f.退出方向。向后推(读取);
^
prog6.cc:174:15:错误:“class std::vector”没有名为
“出口锁定”
房间出口锁定。推回(读取);
^
房间\u f是房间向量,但在访问该向量中特定房间的字段之前,必须选择一个房间。您错过了在向量的索引处访问项的步骤
vector<room>[index].field_name
vector[index]。字段名称
您还需要先初始化房间对象,然后才能使用它们。正如错误所述
rooms\u f
是一个向量
,而不是房间
,因此它没有成员退出索引
。是否要一次构建一个房间
,然后将其添加到房间_f
?