I have two .cpp files:
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
#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:
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. Or create vehicles.cpp
and move definitions there.
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.