[英]Separating the code in header.h, main.cpp and implementation.cpp
I'm trying to separate my code into header.h, main.cpp and implementation.cpp, but I'm not getting it.我试图将我的代码分成 header.h、main.cpp 和 implementation.cpp,但我没有得到它。 Can someone help me?
有人能帮我吗? I need that separate code in these files, and that the game works properly.
我需要在这些文件中使用单独的代码,并且游戏可以正常运行。 I've tried in several ways, but I'm not getting.If I try to run as is, the IDE reports "multiple definition of"
我已经尝试了多种方法,但我没有得到。如果我尝试按原样运行,IDE 会报告“多个定义”
main.cpp主程序
#include<iostream>
#include<conio.h>
#include"implem.cpp"
using namespace std;
int main()
{
setup();
while (!gameover) {
mapa ();
input ();
algoritmo ();
}
return 0;
}
#ifndef HEADER_FLAG
#define HEADER_FLAG
void setup();
void mapa();
void input();
void algoritmo();
#endif
implem.cpp实现.cpp
#include<conio.h>
#include"header.h"
#include<stdlib.h>
#include<time.h>
using namespace std;
bool gameover;
const int width = 20;
const int height = 17;
int x, y, fruitX, fruitY, score;
int tailX[100], tailY[100]; //snake coordinates
int nTail;
enum eDirecton {STOP = 0, LEFT,RIGHT, UP, DOWN}; // Controls
eDirecton dir;
void setup(){
gameover = false;
dir = STOP;
x = width / 2;
y = height / 2;
fruitX = rand() % width;
fruitY = rand() % height; score = 0;
}
void mapa(){
system("cls");
for(int i = 0; i < width+2; i++)
cout << "#";
cout << endl ;
for (int i = 0; i < height ; i++) {
for (int j = 0; j < width; j++) {
if (j == 0)
cout << "#"; //walls
if (i == y && j == x)
cout << "*"; // snake tale
else if (i == fruitY && j == fruitX )
cout << "%"; // change it to change the fruit
else {
bool print = false;
for (int k = 0; k< nTail ; k++) {
if (tailX [k] == j && tailY [k] == i) {
cout << "*"; print = true;
}
} if (!print) cout << " ";
}if (j == width -1)
cout << "#";
}cout << endl;
}
for (int i = 0; i< width+2; i++)
cout << "#";
cout << endl;
cout << "Score:" << score << endl ;
}
void input()
{
if (_kbhit ()) {
switch (_getch ()) {
case 'a':
dir = LEFT;
break;
case 'd':
dir = RIGHT;
break;
case 'w':
dir = UP;
break;
case 's':
dir = DOWN ;
break;
case 'x':
gameover = true;
break;
}
}
}
void algoritmo(){
int prevX = tailX [0];
int prevY = tailY [0];
int prev2X, prev2Y;
tailX[0] = x;
tailY[0] = y;
for(int i = 1;i < nTail ; i++) {
prev2X = tailX[i];
prev2Y = tailY[i];
tailX[i] = prevX;
tailY[i] = prevY;
prevX = prev2X;
prevY = prev2Y ;
}
switch (dir) {
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case UP:
y--;
break;
case DOWN:
y++;
break;
default:
break;
}
if (x >= width) x =0;else if (x <0) x = width -1;
if (y >= height) y = 0; else if (y < 0) y = height - 1;
for (int i =0; i< nTail ;i++)
if (tailX[i] == x && tailY[i] == y)
gameover = true;
if (x == fruitX && y == fruitY) {
score +=10;
fruitX = rand() % width;
fruitY = rand() % height;
nTail ++;
}
}```
I suspect that the multiple definition error is due to the fact that the project has main.cpp
and implem.cpp
.我怀疑多重定义错误是由于项目有
main.cpp
和implem.cpp
。 Since you also #include "implem.cpp"
in main, symbols from implem.cpp
have two origins, and the linker complains.既然你也
#include "implem.cpp"
主,从符号implem.cpp
有两个起源,和链接器抱怨。
Never ever #include
an implementation code.永远不要
#include
实现代码。 The correct way to structure your project is:构建项目的正确方法是:
// main.cpp
#include "header.h"
int main()
{
setup();
while (!gameover) {
mapa ();
input ();
algoritmo ();
}
return 0;
}
header.h
and implem.cpp
should remain as they are. header.h
和implem.cpp
应保持原样。
Notice that you don't need to #include
anything else in main
, because it doesn't refer to the symbols declared in either iostream
or conio.h
.请注意,您不需要在
main
#include
任何其他内容,因为它不引用在iostream
或conio.h
声明的符号。
You're #include
ing implem.cpp
into main.cpp instead of implem.h
.你
#include
ing implem.cpp
到 main.cpp 而不是implem.h
。
Probably you've also added implem.cpp to the project in your IDE.可能您还向 IDE 中的项目添加了implem.cpp。 Then it's compiled on its own, and a second time as included part of main.cpp.
然后它自己编译,第二次编译为 main.cpp 的包含部分。 When the IDE links the compiled object files, all the stuff in implem.cpp will be doubly defined.
当 IDE 链接编译后的目标文件时,implem.cpp 中的所有内容都将被双重定义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.