C++ 从结构实例调用变量语法问题C++;
虽然我不喜欢问一个我不知道的简单语法问题,但作业截止日期迫在眉睫,我完全被难住了,我查到的信息似乎都没有用处。我在头文件中有一个结构:C++ 从结构实例调用变量语法问题C++;,c++,syntax,struct,instance,C++,Syntax,Struct,Instance,虽然我不喜欢问一个我不知道的简单语法问题,但作业截止日期迫在眉睫,我完全被难住了,我查到的信息似乎都没有用处。我在头文件中有一个结构: struct roomStruct { // The room the player is currently in, and its interactive objects. string roomName; string size; int exits[dirnum]; bool rustyKeyIn; boo
struct roomStruct
{
// The room the player is currently in, and its interactive objects.
string roomName;
string size;
int exits[dirnum];
bool rustyKeyIn;
bool goldKeyIn;
bool copperKeyIn;
bool rustyDoorIn;
bool goldDoorIn;
bool copperDoorIn;
bool torchIn;
bool toyIn;
bool leverIn;
bool brokenDoorIn;
bool oldCheeseIn;
bool toyBoxIn;
bool skeletonIn;
bool ghostIn;
};
和一个这样的结构实例(每个gen_rooms编号有更多,格式相同):
然后,我尝试从上面的实例中调用roomName变量,但我不知道如何实现。我知道这是一个丑陋的文本冒险输入法,但这不是当前的问题
int main()
{
string verb;
string noun;
roomStruct rooms[roomnum];
genRooms(rooms);
int currentRoom = entrance;
cout << "Welcome to GHOST HOUSE!!!" << endl << endl << "Please enter commands in the format 'verb -> enter -> noun -> enter' in all lower case." << endl << endl;
// All commands will be in the format 'verb, noun', with the noun not being required for all commands. Currently, lower case is required.
cout << "You are in the " << roomStruct.roomName << "." << endl;
cout << "What do you want to do?" << endl;
cin >> verb >> noun;
intmain()
{
字符串动词;
字符串名词;
roomStruct房间[roomnum];
房间;
室内=入口;
不能这样做:
int main(){
string verb;
string noun;
roomStruct rooms[roomnum];
genRooms(rooms);
int currentRoom = entrance;
cout << "Welcome to GHOST HOUSE!!!" << endl << endl << "Please enter commands in the format 'verb -> enter -> noun -> enter' in all lower case." << endl << endl;
// All commands will be in the format 'verb, noun', with the noun not being required for all commands. Currently, lower case is required.
cout << "You are in the " << rooms[currentRoom].roomName << "." << endl;
cout << "What do you want to do?" << endl;
cin >> verb >> noun;
intmain(){
字符串动词;
字符串名词;
roomStruct房间[roomnum];
房间;
室内=入口;
cout@Triiby>>您是否将“rms”存储在任何地方?如果没有,那么您如何从主功能访问它?不太清楚您的意思,但实例(带有rms的实例)位于.cpp文件中,而不是标头中,如果这有帮助的话。它确实存在,谢谢:)毕竟可能能够及时编写此内容!
int main(){
string verb;
string noun;
roomStruct rooms[roomnum];
genRooms(rooms);
int currentRoom = entrance;
cout << "Welcome to GHOST HOUSE!!!" << endl << endl << "Please enter commands in the format 'verb -> enter -> noun -> enter' in all lower case." << endl << endl;
// All commands will be in the format 'verb, noun', with the noun not being required for all commands. Currently, lower case is required.
cout << "You are in the " << rooms[currentRoom].roomName << "." << endl;
cout << "What do you want to do?" << endl;
cin >> verb >> noun;