是否在'{'标记之前不允许使用函数定义? [英] Is a function definition not allowed here before a '{' token?

查看:134
本文介绍了是否在'{'标记之前不允许使用函数定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am creating a game, and I have this code. However, it is not working:

#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:在函数"int main()"中:
Main.cpp:36:17:错误:预期在"{"令牌之前的;"
Main.cpp:67:1:错误:输入末尾应为'}'

错误的错误消息
将代码编辑为当前代码.

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

Wrong Error Message
Edited code to current.

推荐答案

您正在声明主函数内部的函数体,这是无效的.另外,您使用了太多的'}'-s.

You are declaring the function bodies inside the main function, which is not valid. Also you've been using too many '}'-s.

您的代码应如下所示:

#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;
}

这篇关于是否在'{'标记之前不允许使用函数定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆