新写了一个dev-c++的小游戏,struct Room有点问题,大佬可以帮助该一下吗?
问题代码段
/* ---------- 房间 ---------- */
struct Room {
string description;
map<string, Room*> exits;
bool hasSecretWall = false;
Room(string desc, map<string, Room*> ext, bool secret = false)
: description(desc), exits(ext), hasSecretWall(secret) {}
};
全部代码
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <windows.h>
#include <algorithm>
using namespace std;
/* ---------- 颜色 ---------- */
const int DEFAULT_COLOR = 7;
const int ROOM_COLOR = 10;
const int ITEM_COLOR = 14;
const int COMMAND_COLOR = 11;
const int WIN_COLOR = 12;
const int LOSE_COLOR = 12;
const int ACTION_COLOR = 13;
const int ERROR_COLOR = 12;
void setColor(int color) {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, color);
}
void clearScreen() { system("cls"); }
/* ---------- 房间 ---------- */
struct Room {
string description;
map<string, Room*> exits;
bool hasSecretWall = false;
Room(string desc, map<string, Room*> ext, bool secret = false)
: description(desc), exits(ext), hasSecretWall(secret) {}
};
/* ---------- 全局变量 ---------- */
string notebook = "北门不是摆设吧……" ;
bool hasNotebook = false;
const string NORTH_CODE = "90843"; // 北门密码
bool northDoorOpen = false; // 北门是否已打开
/* ---------- 创建世界 ---------- */
void createWorld(Room*& startRoom,
Room*& treasureRoom,
Room*& studyRoom,
Room*& kitchen)
{
/* 大厅(北门改为密码锁) */
Room* hall = new Room{
"你站在大厅,四周有发霉的墙壁。东边是书房,西边是厨房。\n"
"北门上有一个 5 位数字密码锁。",
{{"east", NULL}, {"west", NULL}}
};
/* 书房 */
studyRoom = new Room{
"书房满是灰尘,书架倒在地上。桌上有个[箱子]上着小锁,旁边有一本[笔记本]。"
"北边有一面墙,看起来破败不堪,墙上的裂缝中有些许光亮。南边返回大厅。",
{{"south", NULL}},
true
};
/* 厨房 */
kitchen = new Room{
"厨房散发着腐臭。灶台上有把[小刀]。东边返回大厅。",
{{"east", NULL}}
};
/* 宝藏房 */
treasureRoom = new Room{
"★★★ 真正的宝藏 ★★★\n"
"你打开了北门,走进隐藏的密室……\n"
"墙壁上刻着古老的文字:\n"
"「真正的宝藏不在金银,而在冒险本身」\n"
"按回车结束游戏...",
{}
};
/* 连接房间 */
hall->exits["east"] = studyRoom;
hall->exits["west"] = kitchen;
studyRoom->exits["south"] = hall;
kitchen->exits["east"] = hall;
startRoom = hall;
}
/* ---------- 小工具 ---------- */
string toLower(const string& str) {
string s = str;
transform(s.begin(), s.end(), s.begin(), ::tolower);
return s;
}
/* ---------- 笔记本 ---------- */
void showNotebook() {
if (!hasNotebook) {
setColor(ERROR_COLOR);
cout << "你还没有拿到笔记本。\n";
return;
}
setColor(ITEM_COLOR);
cout << "\n=== 笔记本 ===\n";
cout << notebook << "\n";
}
/* ---------- 主循环 ---------- */
int main() {
Room *currentRoom, *treasureRoom, *studyRoom, *kitchen;
bool hasKey = false, hasKnife = false, boxOpened = false;
bool gameWon = false;
createWorld(currentRoom, treasureRoom, studyRoom, kitchen);
clearScreen();
setColor(ROOM_COLOR);
cout << "=== 古宅探险 ===\n";
setColor(COMMAND_COLOR);
cout << "命令: go [方向], look, get [物品], use [物品], notebook, use keypad, quit\n";
while (true) {
if (gameWon) break;
setColor(ROOM_COLOR);
cout << "\n" << currentRoom->description << "\n";
cout << "> ";
string cmd; getline(cin, cmd);
string low = toLower(cmd);
if (low == "quit") {
cout << "再见!\n"; break;
}
else if (low == "look") {
clearScreen();
cout << currentRoom->description << "\n";
}
/* 方向 */
else if (low.find("go ") == 0) {
string dir = low.substr(3);
if (dir == "north" && currentRoom->description.find("密码锁") != string::npos) {
if (!northDoorOpen) {
setColor(ERROR_COLOR);
cout << "北门紧锁,需要先输入正确密码。\n";
continue;
}
}
if (currentRoom->exits.count(dir)) {
currentRoom = currentRoom->exits[dir];
} else {
setColor(ERROR_COLOR);
cout << "没有路!\n";
}
}
/* 拿物品 */
else if (low == "get notebook" && currentRoom == studyRoom && !hasNotebook) {
if (currentRoom->description.find("[笔记本]") != string::npos) {
hasNotebook = true;
size_t pos = currentRoom->description.find("[笔记本]");
currentRoom->description.erase(pos, 6);
setColor(ACTION_COLOR);
cout << "你拿到了笔记本。\n";
}
}
else if (low == "get knife" && currentRoom == kitchen && !hasKnife) {
if (currentRoom->description.find("[小刀]") != string::npos) {
hasKnife = true;
size_t pos = currentRoom->description.find("[小刀]");
currentRoom->description.replace(pos, 4, "空的灶台");
setColor(ITEM_COLOR);
cout << "你捡起了小刀。\n";
}
}
else if (low == "get key" && currentRoom == studyRoom && boxOpened && !hasKey) {
hasKey = true;
size_t pos = currentRoom->description.find("钥匙");
currentRoom->description.replace(pos, 3, "空的箱子");
setColor(ITEM_COLOR);
cout << "你拿到了钥匙!\n";
}
/* 箱子与假墙 */
else if (low == "open box" && currentRoom == studyRoom && !boxOpened) {
if (hasKnife) {
boxOpened = true;
size_t pos = currentRoom->description.find("小锁");
currentRoom->description.replace(pos, 3, "钥匙");
setColor(ACTION_COLOR);
cout << "你用刀撬开了箱子!\n";
} else {
setColor(ERROR_COLOR);
cout << "需要工具才能打开箱子。\n";
}
}
else if (low == "use knife" && currentRoom == studyRoom && hasKnife) {
if (studyRoom->hasSecretWall) {
studyRoom->hasSecretWall = false;
studyRoom->exits["north"] = treasureRoom;
size_t pos = currentRoom->description.find("假墙");
currentRoom->description.replace(pos, 3, "破洞");
setColor(ACTION_COLOR);
cout << "你用刀划开了假墙,出现了隐藏的通道!\n";
} else {
setColor(ERROR_COLOR);
cout << "这里没有需要破坏的东西。\n";
}
}
/* 北门密码锁 */
else if (low == "use keypad" && currentRoom->description.find("密码锁") != string::npos) {
setColor(COMMAND_COLOR);
cout << "请输入 5 位数字密码:";
string guess; cin >> guess; cin.ignore();
if (guess == NORTH_CODE) {
northDoorOpen = true;
currentRoom->exits["north"] = treasureRoom;
setColor(ACTION_COLOR);
cout << "密码正确!北门缓缓打开。\n";
} else {
setColor(ERROR_COLOR);
cout << "密码错误,门锁毫无反应。\n";
}
}
/* 笔记本 */
else if (low == "notebook") {
showNotebook();
}
else {
setColor(ERROR_COLOR);
cout << "无效命令!\n";
}
}
/* ---------- 结局 ---------- */
if (currentRoom == treasureRoom && northDoorOpen) {
clearScreen();
setColor(WIN_COLOR);
cout << treasureRoom->description;
cin.ignore();
}
/* ---------- 清理 ---------- */
delete studyRoom;
delete kitchen;
delete treasureRoom;
delete currentRoom;
setColor(DEFAULT_COLOR);
return 0;
}
问题描述
52 5 C:\Users\86138\Desktop\c++\01c++_try\game_try.cpp [Error] no matching function for call to 'Room::Room(<brace-enclosed initializer list>)'
60 5 C:\Users\86138\Desktop\c++\01c++_try\game_try.cpp [Error] no matching function for call to 'Room::Room(<brace-enclosed initializer list>)'
66 5 C:\Users\86138\Desktop\c++\01c++_try\game_try.cpp [Error] no matching function for call to 'Room::Room(<brace-enclosed initializer list>)'
备注:为Dev-c++ 5.11
7104

被折叠的 条评论
为什么被折叠?



