gpt4 book ai didi

c++ - 之前在这里定义的 Codeblocks int main

转载 作者:行者123 更新时间:2023-11-28 02:36:01 25 4
gpt4 key购买 nike

尝试在这里和 devc++ 中制作一个基本的角色扮演游戏时,我遇到了各种关于没有 % 的 win 文件错误。停止。

所以我切换到代码块,现在即使是基础知识也无法编译。错误是这样的:

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|

我不知道那是什么意思,它对几乎所有数据类型都这样做(我的数组 ints bool 等我不确定正确的措辞)我检查过没有其他主要功能(我确定我不会那样做毕竟这不是 java)。我确实有一个头文件,我确保将它们添加到我的项目文件中。

这是我的源代码

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

我想补充一点,我知道我没有在 main.cpp 中实现类,但我正在退后一步尝试让它至少运行。在我创建 getter 之前,一切都在 devcpp 中运行。

最佳答案

您还在头文件中包含了 main.cpp,导致函数 main 被定义了两次。请记住,使用 #include 基本上是将整个文件复制并粘贴到其中。

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

cpp 文件中会包含h 文件。美好的。但是,h 文件包含了 cpp 文件!这意味着当 main.cpp 解析通过 #include 时,其内容已被解析一次,并为您提供信息性编译错误。尝试用实际复制和粘贴内容替换 #include,您会看到发生了什么。

通常,您希望将 .h 文件包含到 .cpp 文件中,而不是相反。实现需要了解您的类,但反之则不然。

在伪代码中,每个缩进级别代表一个文件:

// 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

关于c++ - 之前在这里定义的 Codeblocks int main,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27418067/

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