0

Can't understand what doesn't this freak(the Linker). I tried to find answer in google but usually people do connect several times the same file. I have the same Player.cpp connects once in the main file, and inside Player.cpp connected Player.h, that's all. What's wrong?

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;
};
valiano
  • 16,433
  • 7
  • 64
  • 79
  • 1
    Your bug is here ***#include "Player.cpp"*** Never include a .cpp file. – drescherjm Jan 02 '18 at 01:48
  • Do not include cop, just include the .h file. If the cop isin the same project, it will linked automatically – Md Monjur Ul Hasan Jan 02 '18 at 01:51
  • And what do I need to attend .cpp inside .h ? Just Visual Studio itself has generated me a file where .h pulled out .cpp, so I decided that connects .cpp – Luxoftimus Jan 02 '18 at 01:51
  • `Player.cpp` is already part of your project. From your output I can see it was being compiled so its already there. Instead of including a .cpp file you make it part of your project. – drescherjm Jan 02 '18 at 01:53
  • 1
    Advice -- do not call your variable `vector`, and especially since you have `using namespace std;`. If you happen to change your code to actually use `std::vector`, you will be in a hurt of compiler errors. – PaulMcKenzie Jan 02 '18 at 02:36
  • _"Can't understand what doesn't this freak(the Linker)."_ is not a problem description... Do you mean 'I can't understand why the linker gives me the error quoted in my title'? If so, you should quote the full error(s) in your post, not a vague excerpt of them. – underscore_d Jan 03 '18 at 13:53

1 Answers1

2

Include the Player.h header in your ConsoleApplication1.cpp source file:

#include "Player.h"

and remove the Player.cpp source file from your main.h header which is the cause of multiple definitions:

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

Declarations should go into header files, definitions should go inside source files. Source files (*.cpp) should include header files (*.h), not the other way around.

Ron
  • 14,674
  • 4
  • 34
  • 47
  • `Player.cpp` should include `Player.h`. Also `Player.cpp` should not contain `#pragma once` with that said your question had the correct `Player.cpp` I am a little confused at the code in the picture. – drescherjm Jan 02 '18 at 02:06
  • Hm, now the compiler says that there is no such class `'Player': is not a class or namespace name` Player.cpp `#include "stdafx.h"` and `#include "Player.h"` Player.h `#pragma once` and `#include "Player.cpp"` – Luxoftimus Jan 02 '18 at 02:16
  • Is the contents of Player.h the exact same as you have posted in your question? – drescherjm Jan 02 '18 at 02:20
  • 1
    Also again delete the line `#include "Player.cpp"` – drescherjm Jan 02 '18 at 02:21
  • I think I deleted it .from cpp .h, and everything works. But I don't see how it connects. The linker automatically finds the needed cpp ? I don't put it clearly – Luxoftimus Jan 02 '18 at 02:25
  • In Visual Studio the linker will link all objects generated from the source files that are part of your project. – drescherjm Jan 02 '18 at 02:30