gpt4 book ai didi

c++ - 已在 ConsoleApplication1.obj 中定义

转载 作者:行者123 更新时间:2023-11-28 01:39:13 24 4
gpt4 key购买 nike

<分区>

无法理解这个怪胎(链接器)不是什么。我试图在谷歌中找到答案,但通常人们会多次连接同一个文件。我在主文件中有相同的 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) {

}
}

主要.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 };

播放器.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;
}

播放器.h:

#pragma once

class Player
{
public:
Player(int x, int y);
~Player();
int getX();
int getY();
private:
int x;
int y;
};

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