错误LNK2005对象已定义

时间:2014-10-23 07:41:41

标签: c++ class multiple-definition-error

我有两个.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);
    //[...]
}

和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;
}
//[...]

以及头文件:

#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;
}

编译器(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

errors

我知道这是一个关于此错误的常见问题,但我尝试删除它,但没有成功。

1 个答案:

答案 0 :(得分:2)

tanks::shoot()tanks::info()移至terrain.cppclass.cpp文件。或者创建vehicles.cpp并在那里移动定义。

您从两个源文件中包含vehicles.h,因此两个目标文件(class.objterrain.obj)都定义了tanks::shoot()tanks::info(),但应该是only one definition,因此错误。