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

标签 c++ linker-errors

<分区>

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

最佳答案

在您的 ConsoleApplication1.cpp 源文件中包含 Player.h header :

#include "Player.h"

并从 main.h header 中删除 Player.cpp 源文件,这是多个定义的原因:

#include "Player.cpp"  // <- remove this line

声明应该放在头文件中,定义应该放在源文件中。源文件 (*.cpp) 应该包含头文件 (*.h),而不是相反。

关于c++ - 已在 ConsoleApplication1.obj 中定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48054681/

相关文章:

c++ - 如何解释 array[value - 'a' ]

c++ - 链接器错误 'unresolved external symbol' : working with templates

templates - MFC 项目导致 "multiple definition"链接器错误?

xcode - Swift 3 的链接器命令失败,退出代码为 1(使用 -v 查看调用)

c++ - 什么是 undefined reference /未解析的外部符号错误以及如何修复它?

c++ - 在 Qt 和 C++ [QMediaPlayer] 中更改 mp3 速度

c++ - 异常处理(限制异常)

c# - 从非托管 C++ 调用 C# 传递或返回 "Complex"类型

c++ - 如何处理 Boost.Spirit 生成的警告?

c - 从命令行使用 Visual Studio 2013 编译 aritchk.c 时出现未解析的符号