gpt4 book ai didi

c++ - 在 '{' 标记之前不允许在此处定义函数吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:07:31 32 4
gpt4 key购买 nike

我正在创建一个游戏,并且我有这段代码。但是,它不起作用:

#include<iostream>
using namespace std;

const int MAX_ITEMS = 100;
bool running = 1;
int playerInfo[2];
void titleFunc();
int userInput = 0;

void newGameFunc();

int main() {
titleFunc();
newGameFunc();
int playerLocation = 0;
while (running) {

}

if (playerLocation == 1) {
cout << "You are in a dungeon. You have just woke up from escaping the execution of your father. You see a pathway to the North, and a large gaping hole to the South.\n";
cout << "1. Go South\n 2. Go North";
cin >> userInput;
if (userInput == 1)
playerLocation = 2;
else
if (userInput == 2)
playerLocation = 3;
}
return 0;

titleFunc() {
cout << "\t\t\t\t---Fantasee---\n\n\n";
cout << "\t\t\t\t 1:Play\n";

cin >> userInput;

if (userInput == 1) {
newGameFunc();
}
else {
running = 0;
}
return;
}

newGameFunc() {
cout << "Welcome to Fantasee, a world of adventure and danger. \n";
cout << "To begin, please enter your gender: \n 1. Male 2. Female";
cin >> userInput;
playerInfo[0] = userInput;

cout << "And what class do you wish to be? \n 1. Wizard 2. Archer 3. Warrior 4. Trickster 5. Knight 6. Assassin";
cin >> userInput;
playerInfo[1] = userInput;
playerLocation = 1;
return;
}
}

}
}

我收到错误消息:

g++ Main.cpp -o Main
Main.cpp: In function ‘int main()’:
Main.cpp:36:17: error: expected ‘;’ before ‘{’ token
Main.cpp:67:1: error: expected ‘}’ at end of input

Edit: Wrong Error Message
Edited code to current.

最佳答案

您在主函数内声明函数体,这是无效的。此外,您使用了过多的 '}'。

您的代码应该更像这样:

#include<iostream>
using namespace std;

const int MAX_ITEMS = 100;

bool running = 1;

int playerInfo[2];

void titleFunc();

int userInput = 0;
int playerLocation = 0;

void newGameFunc();

void titleFunc() {
cout << "\t\t\t\t---Fantasee---\n\n\n";
cout << "\t\t\t\t 1:Play\n";

cin >> userInput;

if (userInput == 1) {

newGameFunc();

}
else {
running = 0;
}
return;
}

void newGameFunc() {
cout << "Welcome to Fantasee, a world of adventure and danger. \n";
cout << "To begin, please enter your gender: \n 1. Male 2. Female";
cin >> userInput;
playerInfo[0] = userInput;

cout << "And what class do you wish to be? \n 1. Wizard 2. Archer 3. Warrior 4. Trickster 5. Knight 6. Assassin";
cin >> userInput;
playerInfo[1] = userInput;
playerLocation = 1;
return;
}

int main() {

titleFunc();

newGameFunc();

while (running) {

}
if (playerLocation == 1){
cout << "You are in a dungeon. You have just woke up from escaping the execution of your father. You see a pathway to the North, and a large gaping hole to the South.\n";
cout << "1. Go South\n 2. Go North";
cin >> userInput;
if (userInput == 1) playerLocation = 2;
else if (userInput == 2) playerLocation = 3;
}
return 0;
}

关于c++ - 在 '{' 标记之前不允许在此处定义函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22185288/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com