简体   繁体   English

先前在此处定义的代码块int main

[英]Codeblocks int main previously defined here

Trying to make a basic rpg here and in devc++ I was getting all sorts of win file errors about not having a %. 试图在这里和在devc ++中制作一个基本的rpg,我遇到了各种有关没有%的win文件错误的信息。 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). 我不知道这意味着什么,并且它几乎针对每种数据类型(我的数组int bool等,我不确定正确的语言)都这样做了。我已经检查了没有其他主要功能(确保我在所有这些之后都不会这样做)不是Java)。 I do have a header file and i made sure to add them to my project file. 我确实有头文件,并且确保将它们添加到我的项目文件中。

Heres my source code 这是我的源代码

main.cpp 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 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. 我想补充一点,我知道我没有在main.cpp中隐含该类,但我退后一步尝试使其至少正常运行。 Until I created the getters everything ran in devcpp. 在创建吸气剂之前,一切都在devcpp中运行。

You include main.cpp in the header file as well, causing the function main to be defined twice. main.cpp包括在头文件中,从而导致函数main被定义两次。 Remember that using #include is basically copying and pasting the whole file in there. 请记住,使用#include基本上是在其中复制并粘贴整个文件。

#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. 发生的情况是在cpp文件中将包含h文件。 Fine. 精细。 But then, the h file includes the cpp file back! 但是然后, h文件将cpp文件包括在内! 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. 这意味着当main.cpp解析超过#include其内容已经被解析了一次,并为您提供了有用的编译错误。 Try replacing the #include s with actually copying and pasting the contents, and you'll see what happens. 尝试用实际复制和粘贴内容替换#include ,您将看到会发生什么。

Generally, you want to include the .h file into the .cpp file, and not the other way around. 通常,您希望将.h文件包含在.cpp文件中,而不是相反。 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

What happens here is that in main.cpp you have: 这里发生的是在main.cpp您有:

#include "createcharacter.h"

which contains your class definition. 其中包含您的类定义。 In createcharacter.h however, you have 但是,在createcharacter.h中,

#include "main.cpp"

including cpp files is frankly nonstandard and usually a sign of a mistake somewhere. 坦白说,包括cpp文件是非标准的,通常是某处错误的标志。 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. 由于main.cpp没有包含在头罩中,因此预处理器会再次粘贴完整的代码文件,这将导致main.cpp所有符号都被定义两次,从而导致您看到错误。

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. 在嵌套的main.cpp ,您再次包含createcharacter.h ,但这是一个空的扩展,因为标头中的包含保护阻止了预处理器再次包含代码。

What you need to remember is that the preprocessor (including the #include directives) only does text substitution, the result of which is then compiled. 您需要记住的是,预处理器(包括#include指令)仅执行文本替换,然后对其结果进行编译。

Just to clarify why you are seeing these errors. 只是为了阐明为什么您会看到这些错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM