[英]Error LNK2005 object already defined
我有兩個.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
我知道這是關於此錯誤的常見問題,但我正在嘗試將其刪除,但未成功。
移動tanks::shoot()
和tanks::info()
到terrain.cpp
或class.cpp
文件。 或創建vehicles.cpp
並在其中移動定義。
你是包括vehicles.h
從兩個源文件,所以這兩個目標文件( class.obj
和terrain.obj
)有tanks::shoot()
和tanks::info()
定義的,但應該有只有一個定義 ,因此,錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.