2014-10-23 2 views
0

У меня есть два .cpp файлы:Ошибка объект LNK2005 уже определен

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)" ([email protected]@@QAEXXZ) already defined in class.obj 
terrain.obj : error LNK2005: "public: void __thiscall tanks::shoot(void)" ([email protected]@@QAEXXZ) already defined in class.obj 
class.exe : fatal error LNK1169: one or more multiply defined symbols found 

errors

Я знаю, что это общий вопрос об этой ошибке, но я пытаюсь удалить его, но безуспешно.

+1

У вас есть оператор if, который, когда он работает для того, что вы делаете, если вы используете код, которым я помогал вам, прежде чем он не будет полностью прав. В set_position это должно быть x <= MAP.size() || y <= MAP [x] .size() –

ответ

2

Перемещение tanks::shoot() и tanks::info() - terrain.cpp или class.cpp. Или создайте vehicles.cpp и переместите определения там.

Вы включая vehicles.h из двух исходных файлов, так что оба объектных файлов (class.obj и terrain.obj) имеют tanks::shoot() и tanks::info() определены, но должно быть only one definition, следовательно, ошибка.

Смежные вопросы