简体   繁体   English

错误LNK2005对象已定义

[英]Error LNK2005 object already defined

I have two .cpp files: 我有两个.cpp文件:

class.cpp class.cpp

#include "stdafx.h"
#include "vehicles.h"
#include <iostream>
#include <time.h>

using MAP_GRID = vector<vector<string>>;
using namespace std;

void print_terrain(MAP_GRID);
void set_position(MAP_GRID &, int, int, vehicles::position, string);
void random_position(MAP_GRID &, int, string);
MAP_GRID create_terrain();

MAP_GRID MAP = create_terrain();

int _tmain(int argc, _TCHAR* argv[])
{
    tanks t34(12, 0.5, 21,6);
    srand(time(NULL));

    //set_position(MAP, 5, 5, t34.pos,"[x]");
    random_position(MAP, 12, "[o]");
    print_terrain(MAP);
    //[...]
}

and terrain.cpp 和terrain.cpp

#include "stdafx.h"
#include <iostream>
#include <vector>
#include "vehicles.h"

#define MIN_SIZE 6
#define MAX_SIZE 15

using std::vector;
using std::string;
using MAP_GRID = vector<vector<string>>;
int global_size;

void set_position(MAP_GRID &MAP, int x, int y, vehicles::position &pos, string object)
{
    if (x <= MAP.size() || y <= MAP.size())
        if (MAP[x][y] != "[ ]")
            std::cout << "\nPosition is occupied" << std::endl;
        else
        {
            MAP[x][y] = object;
            pos.x.push_back(x);
            pos.y.push_back(y);
        }

    else
        std::cout << "\Choose correct position" << std::endl;
}
//[...]

and also header file: 还有头文件:

#include <iostream>
#include <vector>

using namespace std;
using std::vector;

using MAP_GRID = vector<vector<string>>;
void change_position(MAP_GRID &, int, int);

class vehicles{
protected:
    double durability;
    double velocity;
public:
     vehicles(double d, double v) : durability(d), velocity(v) {}
     ~vehicles() {}

     void drive();
     void info() { cout << durability << " " << velocity << "\n"; }

     struct position{
         vector<int> x;
         vector<int> y;
     }pos;


};

class tanks:public vehicles{
private:
    double damage;
public:
    tanks(double dmg, double v, double d, int m) :vehicles(d, v), damage(dmg), ammo(m) {}
    ~tanks() {}
    int ammo;
    void shoot();
    void info();
};

void tanks::shoot(){
    if (ammo >= 0)
    {
        cout << "You deal " << damage << ". You have " << ammo << "ammo left.\n\n";
        ammo-=1;
    }
    else
        cout << "You don't have ammo\n\n";
}

void tanks::info(){
    cout << "You velocity " << velocity << endl;
    cout << "You durability " << durability << endl;
    cout << "You damage " << damage << endl;
}

The compiler (Microsoft Visual Studio 2013) gives me these errors: 编译器(Microsoft Visual Studio 2013)给我这些错误:

terrain.obj : error LNK2005: "public: void __thiscall tanks::info(void)" (?info@tanks@@QAEXXZ) already defined in class.obj
terrain.obj : error LNK2005: "public: void __thiscall tanks::shoot(void)" (?shoot@tanks@@QAEXXZ) already defined in class.obj
class.exe : fatal error LNK1169: one or more multiply defined symbols found

错误

I know it's a common question about this error, but I'm trying remove it, without success. 我知道这是关于此错误的常见问题,但我正在尝试将其删除,但未成功。

Move tanks::shoot() and tanks::info() to terrain.cpp or class.cpp file. 移动tanks::shoot()tanks::info()terrain.cppclass.cpp文件。 Or create vehicles.cpp and move definitions there. 或创建vehicles.cpp并在其中移动定义。

You are including vehicles.h from two source files, so both object files ( class.obj and terrain.obj ) have tanks::shoot() and tanks::info() defined, but there should be only one definition , hence the error. 你是包括vehicles.h从两个源文件,所以这两个目标文件( class.objterrain.obj )有tanks::shoot()tanks::info()定义的,但应该有只有一个定义 ,因此,错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM