Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a function definition not allowed here before a '{' token?

Tags:

c++

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

}
}

And I am getting the error message:

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.

like image 443
user3233136 Avatar asked Mar 04 '14 23:03

user3233136


People also ask

How do you fix a function definition is not allowed here before '{' token?

You have your classes' function definitions inside your main function, which is not allowed. To fix that, you should place them outside, but to do that you will need to place the whole class outside of main as well (since you need it to be in scope): class A { public: void foo(); }; void A::foo() { <...> }

What is a function definition C++?

A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. Functions are used to perform certain actions, and they are important for reusing code: Define the code once, and use it many times.

How do you call a function C++?

A function declaration tells the compiler about a function name and how to call the function. The actual body of the function can be defined separately. int max(int, int); Function declaration is required when you define a function in one source file and you call that function in another file.


1 Answers

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

Your code should look more like this:

#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;
}
like image 92
Geries Avatar answered Sep 24 '22 09:09

Geries