代码块 int main 之前在这里定义
Codeblocks int main previously defined here
试图在这里和devc ++中制作一个基本的RPG,我收到了各种关于没有%的win文件错误。
所以我切换到代码块,现在即使是基础知识也无法编译。错误是这样的:
C:UsersElliott PewDesktoprpg_final6_days_to_escapemain.cpp|10|error: redefinition of 'bool running'| C:UsersElliott PewDesktoprpg_final6_days_to_escapemain.cpp|24|error: redefinition of 'int main()'|C:UsersElliott PewDesktoprpg_final6_days_to_escapemain.cpp|24|error: 'int main()' previously defined here|
我不知道这意味着什么,它几乎适用于所有数据类型(我的数组整数布尔等,我不确定正确的措辞)我检查没有其他主要功能(我确定我不会这样做毕竟这不是 java)。我确实有一个头文件,我确保将它们添加到我的项目文件中。
这是我的源代码
主.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 << "nn";
cout << "t6 Days to Escape!n"; //title
cout << "tt 1: Playn"; //main menu options. The first thing the user sees.
cout << "ttt 2: Exitn";
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);
}
创建字符.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
我想补充一点,我知道我并没有在暗示这门课.cpp但我退后一步,试图让它至少运行。在我创建getter之前,一切都在devcpp中运行。
在头文件中也包含main.cpp
,导致函数main
定义两次。请记住,使用 #include
基本上是将整个文件复制并粘贴到其中。
#include "main.cpp" // "paste" the complete contents of the included file
发生的情况是,在cpp
文件中,将包含h
文件。好。但是,h
文件包含返回的cpp
文件!这意味着当主解析.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
这里发生的事情是,在main.cpp
中,你有:
#include "createcharacter.h"
其中包含您的类定义。但是,createcharacter.h
,您有
#include "main.cpp"
坦率地说,包括CPP文件是不标准的,通常是某处错误的标志。由于 main.cpp 未包含在包含保护中,因此预处理器会再次粘贴完整的代码文件,这会导致main.cpp
中的所有符号被定义两次,从而导致您看到的错误。
在嵌套main.cpp
中,再次包含createcharacter.h
,但这是一个空扩展,因为标头中的包含保护阻止预处理器再次包含代码。
您需要记住的是,预处理器(包括 #include
指令)只执行文本替换,然后编译其结果。
只是为了澄清为什么您会看到这些错误。
- 努力将整数转换为链表。不知道我在这里做错了什么
- 在类定义之后定义一个私有方法
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 如何在自定义类中启用'auto loops'?
- 这个c++代码是如何在没有定义函数的情况下运行的
- 我可以在这里替换什么,因为我不能在 C# 中使用隐式变量的 lambda 函数?
- 当我从下面的代码中删除关键字 virtual 时,它可以正常工作,否则会出现错误。在这里"virtual"字的意义是什么?
- File.cpp.o:OpenPose 标志 CMakeFiles/.. 的多重定义/main.cpp.o:首先在这里定
- 为什么thread_local变量在这里从未初始化?
- 为什么我必须在这里使用dynamic_cast
- C++ 中的自定义异常:在哪里定义它们?
- 在这里,当我们比较 if(vc[i]==vc1[i]) 时,它是向量数组. 实际上比较的值是多少,
- 不允许在类定义之外重写
- 在自定义 std::vector-like 容器中处理指针和非指针模板类型的最佳方法是什么?
- ld: foo.o:foo.h:6 'bar' 的多重定义 foo.h:6: 首先在这里定义
- *在这里定义什么
- 代码块 int main 之前在这里定义
- C ++中的构建错误,未定义的符号,在这里尝试了其他答案,无法弄清楚
- 为什么链接器在这里不产生多个定义错误?
- main()首先在这里定义