Codeblocks int main previously defined here

5.3k Views Asked by At

Trying to make a basic rpg here and in devc++ I was getting all sorts of win file errors about not having a %. stop.

So i switched to codeblocks and now even the basics won't compile. The errors are like this:

C:\Users\Elliott Pew\Desktop\rpg_final\6_days_to_escape\main.cpp|10|error: redefinition of 'bool running'|  C:\Users\Elliott Pew\Desktop\rpg_final\6_days_to_escape\main.cpp|24|error: redefinition of 'int main()'|C:\Users\Elliott Pew\Desktop\rpg_final\6_days_to_escape\main.cpp|24|error: 'int main()' previously defined here|

I have no clue what that means and it is doing it for pretty much every data type(my array ints bool etc im not sure of the proper verbage) Ive checked there is no other main functions(im sure i wouldnt do that after all this isnt java). I do have a header file and i made sure to add them to my project file.

Heres my source code

main.cpp

 //game rpg
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <conio.h>
#include "createcharacter.h"
using namespace std;

//is running check
bool running = 1;

//int healedHP(int x, int y);
//int attackedHP(int x, int y);

//user input var+
int userInput = 0;
int startChoices[4] = {0, 0, 0, 0};
string name;
//function declarations
void theStart();
void newGame();


int main (){

    cout << "Enter your name." << endl;
    cin >> name;
    cout << "Welcome " << name << "." << endl;
    cout << "Strike any key....if you dare......";
    getch();
    system("cls");

    theStart();
    cout << startChoices << endl;

    system("pause");
    return 0;
}
void theStart()
{
    cout << "\n\n";
    cout << "\t6 Days to Escape!\n"; //title
    cout << "\t\t  1: Play\n"; //main menu options. The first thing the user sees.
    cout << "\t\t\t  2: Exit\n";
    cin >> userInput;
    system("cls");
    if(userInput == 1)
    {
        // Create a new game
        newGame();
    }
    else
    {
        //bool then false causeing program to exit
        running = 0;
    }

    return;
}
void newGame(){
    // there are 4 addresses in this array for the following:
    //0. Difficulty
    //1. Class
    //2. Starting Wep
    //3. Boost not implimented yet TODO
    //enum class difficulty{simple, easy, hard, impossible};
    do{
        cout << "Choose Your difficulty: " << endl;
        cout << "\t1. Simple - Game practically plays itself." << endl;
        cout << "\t2. Easy - Not that easy." << endl;
        cout << "\t3. Hard - Zombies do more than crave flesh." << endl;
        cout << "\t4. Impossible - You will not make it." << endl;
        cin >> startChoices[0];
        cout << endl;
        system("cls");
        if(startChoices[0] < 1 || startChoices[0] > 4){
            cout << "Invalid Difficulty Choice. Try again." << endl;}
    }while(startChoices[0] < 1 || startChoices[0] > 4);

    do{
        cout << "Choose your class:" << endl;
        cout << "\t1. Lumber Jack - Stong, hard to kill, but slow." << endl;
        cout << "\t2. Doctor - Healer, weak but fast. Favors health." << endl;
        cout << "\t3. Theif - FAST, Cunning but weak attacks." << endl;
        cout << "\t4. Everydayer - Balenced everything." << endl;
        cin >> startChoices[1];
        cout << endl;
        system("cls");
        if(startChoices[1] < 1 || startChoices[1] > 4){
            cout << "Invalid Class Choice. Try again." << endl;}
    }while(startChoices[1] < 1 || startChoices[1] > 4);

    do{
        cout << "Choose your starting Weapon:" << endl;
        cout << "\t1. Axe" << endl;
        cout << "\t2. Crowbar" << endl;
        cout << "\t3. Swiss army knife" << endl;
        cout << "\t4. Ice pick" << endl;
        cin >> startChoices[2];
        cout << endl;
        if(startChoices[0] < 1 || startChoices[0] > 4){
            cout << "Invalid Weapon Choice. Try again." << endl;}
    }while(startChoices[2] < 1 || startChoices[2] > 4);
    }

createcharacter.h

#ifndef CREATECHARACTER_H
#define CREATECHARACTER_H
#include "main.cpp"
class CreateCharacter{
    public:
        //setter
        void setplayerHealth(int h){
            playerHealth = h;}
        void setplayerMaxHealth(int mh){
            playerMaxHealth = mh;}
        void setplayerStr(int s){
            playerStr = s;}
        void setplayerAgl(int a){
            playerAgl = a;}
        void setplayerInt(int i){
            playerInt = i;}
        void setplayerDifficulty(int d){
            playerDifficulty = d;}

        //getters
        int getHealth(){
            return playerHealth;
        }
        int getMaxHealth(){
            return playerMaxHealth;
        }
        int getStr(){
            return playerStr;
        }
        int getAgl(){
            return playerAgl;
        }
        int getInt(){
            return playerInt;
        }
        int getDifficulty(){
            return playerDifficulty;
        }



    private:
        int playerHealth;
        int playerMaxHealth; //absolute max = 200
        int playerStr; // absolute max = 20
        int playerAgl;// absolute max = 20
        int playerInt;// absolute max = 20
        int playerDifficulty; // absolute max = 4
};

#endif

I want to add that i know im not implimenting the class in the main.cpp but i was stepping back to try and get it to atleast run. Until I created the getters everything ran in devcpp.

2

There are 2 best solutions below

1
On BEST ANSWER

You include main.cpp in the header file as well, causing the function main to be defined twice. Remember that using #include is basically copying and pasting the whole file in there.

#include "main.cpp" // "paste" the complete contents of the included file

What happens is that in the cpp file, the h file will be included. Fine. But then, the h file includes the cpp file back! This means that when the main.cpp parsing gets past the #include its contents have already been parsed once, and gives you the informative compilation error. Try replacing the #includes with actually copying and pasting the contents, and you'll see what happens.

Generally, you want to include the .h file into the .cpp file, and not the other way around. The implementation needs to know about your class, but not the other way around.

In pseudocode, each indentation level represents a file:

// main.cpp
#include createcharacter.h
    // chreatecharacter.h
    has chreatecharacter been included? no! go on.
    #include main.cpp
        // main.cpp
        #include createcharacter.h
            // createcharacter.h
            has createcharacter been included? yes! stop.
        define main
    define createcharacter
define main // <-- oops
1
On

What happens here is that in main.cpp you have:

#include "createcharacter.h"

which contains your class definition. In createcharacter.h however, you have

#include "main.cpp"

including cpp files is frankly nonstandard and usually a sign of a mistake somewhere. Since your main.cpp is not wrapped in include guards, the preprocessor pastes the complete code file again, which causes all symbols in main.cpp to be defined twice, which causes the errors you are seeing.

In the nested main.cpp, you include createcharacter.h again, but this is an empty expansion because the include guards in the header prevent the preprocessor from including the code again.

What you need to remember is that the preprocessor (including the #include directives) only does text substitution, the result of which is then compiled.

Just to clarify why you are seeing these errors.