无法理解这个怪物是什么(链接器)。 我试图在谷歌找到答案,但通常人们连接几次相同的文件。 我有相同的Player.cpp在主文件中连接一次,而在Player.cpp内连接Player.h,就是这样。怎么了?
1>------Rebuild All started : Project: ConsoleApplication1, Configuration : Debug Win32------
1>stdafx.cpp
1>Player.cpp
1>ConsoleApplication1.cpp
1>Generating Code...
1>Player.obj : error LNK2005 : "public: __thiscall Player::Player(int,int)" (? ? 0Player@@QAE@HH@Z) already defined in ConsoleApplication1.obj
1>Player.obj : error LNK2005 : "public: __thiscall Player::~Player(void)" (? ? 1Player@@QAE@XZ) already defined in ConsoleApplication1.obj
1>Player.obj : error LNK2005 : "public: int __thiscall Player::getX(void)" (? getX@Player@@QAEHXZ) already defined in ConsoleApplication1.obj
1>Player.obj : error LNK2005 : "public: int __thiscall Player::getY(void)" (? getY@Player@@QAEHXZ) already defined in ConsoleApplication1.obj
1>C:\Users\New\Documents\Visual Studio 2017\Projects\ConsoleApplication1\Debug\ConsoleApplication1.exe : fatal error LNK1169 : one or more multiply defined symbols found
ConsoleApplication1.cpp:
#include "stdafx.h"
#include "windows.h"
#include <conio.h>
#include <iostream>
#include "main.h"
using namespace std;
void Render(CELL** vector);
void WaitForAction();
void Move(ACTION a);
int main()
{
CELL** vector = new CELL*[SIZE_MAP_Y];
for (int i = 0; i < SIZE_MAP_Y; i++) {
vector[i] = new CELL[SIZE_MAP_X];
for (int j = 0; j < SIZE_MAP_X; j++) {
vector[i][j] = CELL::EMPTY;
}
}
Player* player = new Player(2, 3);
cout << player->getX() << ", ";
cout << player->getY() << endl;
system("pause");
while (true) {
system("cls");
Render(vector);
WaitForAction();
}
for (int i = 0; i < 10; i++)
delete[] vector[i];
delete[] vector;
system("pause");
return 0;
}
void Render(CELL** vector) {
for (int y = 0; y < SIZE_MAP_Y; y++) {
for (int x = 0; x < SIZE_MAP_X; x++) {
if (vector[y][x] == CELL::EMPTY) cout << "#";
else if (vector[y][x] == CELL::PLAYER) cout << "O";
else cout << "?";
}
cout << endl;
}
}
void WaitForAction() {
char ch;
ch = _getch();
if (ch == 'ф' || ch == 'a') Move(ACTION::LEFT);
else if (ch == 'ы' || ch == 's') Move(ACTION::DOWN);
else if (ch == 'в' || ch == 'd') Move(ACTION::RIGHT);
else if (ch == 'ц' || ch == 'w') Move(ACTION::UP);
}
void Move(ACTION a) {
if (a == ACTION::LEFT) {
}
}
main.h:
#include "Player.cpp"
#define SIZE_MAP 10
#ifdef SIZE_MAP_Y
#undef SIZE_MAP_Y
#endif
#ifdef SIZE_MAP_X
#undef SIZE_MAP_X
#endif
#define SIZE_MAP_Y SIZE_MAP
#define SIZE_MAP_X (SIZE_MAP_Y * 2)
enum CELL { EMPTY, PLAYER };
enum ACTION { LEFT, RIGHT, DOWN, UP };
Player.cpp:
#include "stdafx.h"
#include "Player.h"
Player::Player(int x, int y)
{
this->x = x;
this->y = y;
}
Player::~Player()
{
}
int Player::getX() {
return this->x;
}
int Player::getY() {
return this->y;
}
Player.h:
#pragma once
class Player
{
public:
Player(int x, int y);
~Player();
int getX();
int getY();
private:
int x;
int y;
};
答案 0 :(得分:2)
在Player.h
源文件中加入ConsoleApplication1.cpp
标题:
#include "Player.h"
并从Player.cpp
标题中删除导致多个定义的main.h
源文件:
#include "Player.cpp" // <- remove this line
声明应该进入头文件,定义应该放在源文件中。源文件(*.cpp
)应包含头文件(*.h
),而不是相反。