c++ - __thiscall already defined in .obj -


i getting linker errors cannot seem find root cause of, having checked including .cpp files , reading other forums.

the errors are:

1>------ build started: project: penguin_rpg, configuration: debug win32 ------ 1>  main.cpp 1>c:\users\adam\documents\visual studio 2013\projects\penguin_rpg\penguin_rpg\dialogue.hpp(53): warning c4018: '<=' : signed/unsigned mismatch 1>c:\users\adam\documents\visual studio 2013\projects\penguin_rpg\penguin_rpg\main.cpp(39): warning c4244: 'argument' : conversion 'time_t' 'unsigned int', possible loss of data 1>main.obj : error lnk2005: "public: __thiscall area::area(class dialogue,class inventory,class std::vector<class creature *,class std::allocator<class creature *> >)" (??0area@@qae@vdialogue@@vinventory@@v?$vector@pavcreature@@v?$allocator@pavcreature@@@std@@@std@@@z) defined in atlas.obj 1>main.obj : error lnk2005: "public: __thiscall creature::creature(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int,int,int,int,double,unsigned int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0creature@@qae@v?$basic_string@du?$char_traits@d@std@@v?$allocator@d@2@@std@@hhhhni0@z) defined in atlas.obj 1>main.obj : error lnk2005: "public: __thiscall creature::creature(void)" (??0creature@@qae@xz) defined in atlas.obj 1>main.obj : error lnk2005: "public: __thiscall dialogue::dialogue(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >)" (??0dialogue@@qae@v?$basic_string@du?$char_traits@d@std@@v?$allocator@d@2@@std@@v?$vector@v?$basic_string@du?$char_traits@d@std@@v?$allocator@d@2@@std@@v?$allocator@v?$basic_string@du?$char_traits@d@std@@v?$allocator@d@2@@std@@@2@@2@@z) defined in atlas.obj 1>main.obj : error lnk2005: "public: __thiscall item::item(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0item@@qae@v?$basic_string@du?$char_traits@d@std@@v?$allocator@d@2@@std@@0@z) defined in atlas.obj 1>main.obj : error lnk2005: "public: __thiscall weapon::weapon(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,unsigned int,double)" (??0weapon@@qae@v?$basic_string@du?$char_traits@d@std@@v?$allocator@d@2@@std@@0in@z) defined in atlas.obj 1>main.obj : error lnk2005: "public: int __thiscall dialogue::activate(void)" (?activate@dialogue@@qaehxz) defined in atlas.obj 1>main.obj : error lnk2005: "public: void __thiscall inventory::add_armour(class armour *,int)" (?add_armour@inventory@@qaexpavarmour@@h@z) defined in atlas.obj 1>main.obj : error lnk2005: "public: void __thiscall inventory::add_item(class item *,int)" (?add_item@inventory@@qaexpavitem@@h@z) defined in atlas.obj 1>main.obj : error lnk2005: "public: void __thiscall inventory::add_weapon(class weapon *,int)" (?add_weapon@inventory@@qaexpavweapon@@h@z) defined in atlas.obj 1>main.obj : error lnk2005: "public: void __thiscall inventory::clear(void)" (?clear@inventory@@qaexxz) defined in atlas.obj 1>main.obj : error lnk2005: "public: void __thiscall creature::equiparmour(class armour *)" (?equiparmour@creature@@qaexpavarmour@@@z) defined in atlas.obj 1>main.obj : error lnk2005: "public: void __thiscall creature::equipweapon(class weapon *)" (?equipweapon@creature@@qaexpavweapon@@@z) defined in atlas.obj 1>main.obj : error lnk2005: "public: unsigned int __thiscall creature::exptolevel(unsigned int)" (?exptolevel@creature@@qaeii@z) defined in atlas.obj 1>main.obj : error lnk2005: "public: bool __thiscall creature::levelup(void)" (?levelup@creature@@qae_nxz) defined in atlas.obj 1>main.obj : error lnk2005: "public: void __thiscall inventory::merge(class inventory *)" (?merge@inventory@@qaexpav1@@z) defined in atlas.obj 1>main.obj : error lnk2005: "public: void __thiscall inventory::print(bool)" (?print@inventory@@qaex_n@z) defined in atlas.obj 1>main.obj : error lnk2005: "public: int __thiscall inventory::print_armours(bool)" (?print_armours@inventory@@qaeh_n@z) defined in atlas.obj 1>main.obj : error lnk2005: "public: int __thiscall inventory::print_items(bool)" (?print_items@inventory@@qaeh_n@z) defined in atlas.obj 1>main.obj : error lnk2005: "public: int __thiscall inventory::print_weapons(bool)" (?print_weapons@inventory@@qaeh_n@z) defined in atlas.obj 1>main.obj : error lnk2005: "public: void __thiscall inventory::remove_armour(class armour *,int)" (?remove_armour@inventory@@qaexpavarmour@@h@z) defined in atlas.obj 1>main.obj : error lnk2005: "public: void __thiscall inventory::remove_item(class item *,int)" (?remove_item@inventory@@qaexpavitem@@h@z) defined in atlas.obj 1>main.obj : error lnk2005: "public: void __thiscall inventory::remove_weapon(class weapon *,int)" (?remove_weapon@inventory@@qaexpavweapon@@h@z) defined in atlas.obj 1>main.obj : error lnk2005: "public: void __thiscall area::search(class creature &)" (?search@area@@qaexaavcreature@@@z) defined in atlas.obj 1>c:\users\adam\documents\visual studio 2013\projects\penguin_rpg\debug\penguin_rpg.exe : fatal error lnk1169: 1 or more multiply defined symbols found ========== build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

atlas.hpp:

#pragma once #ifndef atlas_hpp #define atlas_hpp  #include <vector>  #include "creature.hpp" #include "item.hpp" #include "weapon.hpp" #include "armour.hpp" #include "area.hpp"  //atlas building functions. atlases contain vectors of //game data not modified. replaced //functions read config files.  void buildatlas_creature(std::vector<creature>& atlas); void buildatlas_item(std::vector<item>& atlas); void buildatlas_weapon(std::vector<weapon>& atlas); void buildatlas_armours(std::vector<armour>& atlas); void buildatlas_area(std::vector<area>& atlas,     std::vector<item>& items, std::vector<weapon>& weapons,     std::vector<armour>& armours, std::vector<creature>& creatures);  #endif //atlas_hpp 

atlas.cpp:

#include "atlas.hpp"  void buildatlas_creature(std::vector<creature>& atlas) {     //fill atlas     //creature(name, health, str, end, dex, hitrate, level)     atlas.push_back(creature("rat", 8, 8, 8, 12, 2.0, 1));      return; } void buildatlas_item(std::vector<item>& atlas) {     //item(name, description)     atlas.push_back(item("gold coin",          "a small disc made of lustrous metal"));     atlas.push_back(item("iron key",         "a heavy iron key simple cut"));      return; } void buildatlas_weapon(std::vector<weapon>& atlas) {     //weapon(name, description, damage, hitrate)     atlas.push_back(weapon("iron dagger",          "a short blade made of iron leather hilt",         5, 10.0));     atlas.push_back(weapon("excalibur",         "the legendary blade, bestowed upon lady of lake",         35, 35.0));      return; } void buildatlas_armours(std::vector<armour>& atlas) {     //armour(name, description, defence, slot)     atlas.push_back(armour("leather vest",         "torso armour made of tanned hide",         4, armour::slot::torso));      return; } void buildatlas_area(std::vector<area>& atlas,     std::vector<item>& items, std::vector<weapon>& weapons,     std::vector<armour>& armours, std::vector<creature>& creatures) {     // area definitions more complicated:     atlas.push_back(area(dialogue(        // standard dialogue definiton         "you in room 1",              // description         { "go room 2", "search" }),      // choices         inventory(                        // area inventory     {         std::make_pair(&items[0], 5)  // pair of item , quantity      },     {         std::make_pair(&weapons[0], 1)// pair of weapon , quantity     },     {         std::make_pair(&armours[0], 1) // pair of armour , quantity     }),     {                                 // creatures     }));      atlas.push_back(area(dialogue(         "you in room 2",         { "go room 1", "search" }),         inventory(     {         std::make_pair(&items[0], 10),         std::make_pair(&items[1], 1)     },     {     },     {     }),     {         &creatures[0]     }));      return; } 

inventory.hpp:

#pragma once #ifndef inventory_hpp #define inventory_hpp  #include "item.hpp" #include "weapon.hpp" #include "armour.hpp"  #include <list> #include <utility> #include <iostream>  class inventory { public:     //whilst weapons , armour items, have own      //specific properties , can't stored in same list.     //the first element of pair stores pointer item     //in item/weapon/armour atlas, defined in main(), second     //if quantity of item      std::list<std::pair<item*, int>> items;     std::list<std::pair<weapon*, int>> weapons;     std::list<std::pair<armour*, int>> armours;      inventory(){};     inventory(std::list<std::pair<item*, int>> items,         std::list<std::pair<weapon*, int>> weapons,         std::list<std::pair<armour*, int>> armours)     {         this->items = items;         this->weapons = weapons;         this->armours = armours;     }      void add_item(item* item, int count);     void add_weapon(weapon* weapon, int count);     void add_armour(armour* armour, int count);      void remove_item(item*, int count);     void remove_weapon(weapon*, int count);     void remove_armour(armour*, int count);      void merge(inventory* inventory);     void clear();      int print_items(bool label);     int print_weapons(bool label);     int print_armours(bool label);     void print(bool label); };  //add item inventory void inventory::add_item(item* item, int count) {     // increase quantity if item exists     (auto& : this->items)     {         if (it.first == item) it.second += count;         return;     }     // if item doesn't exist in inventory,     // pair must created     this->items.push_back(std::make_pair(item, count)); } void inventory::add_weapon(weapon* weapon, int count) {     (auto& : this->weapons)     {         if (it.first == weapon)         {             it.second += count;             return;         }     }     this->weapons.push_back(std::make_pair(weapon, count)); }  void inventory::add_armour(armour* armour, int count) {     (auto& : this->armours)     {         if (it.first == armour)         {             it.second += count;             return;         }     }     this->armours.push_back(std::make_pair(armour, count)); }  void inventory::remove_item(item* item, int count) {     // iterate through items, , if found decrease     // quantity quantity removed     (auto& : this->items)     {         if (it.first == item) it.second -= count;     }     // iterate through list again, , remove elements     // list have 0 or less quantity     // in 2 passes because removing element     // list during loop invalidates iterators, ,     // loop stops working     this->items.remove_if([](std::pair<item*, int>& element)     {         return element.second < 1;     }); }  void inventory::remove_weapon(weapon* weapon, int count) {     (auto& : this->weapons)     {         if (it.first == weapon) it.second -= count;     }     this->weapons.remove_if([](std::pair<weapon*, int>& element)     {         return element.second < 1;     }); }  void inventory::remove_armour(armour* armour, int count) {     (auto& : this->armours)     {         if (it.first == armour) it.second -= count;     }     this->armours.remove_if([](std::pair<armour*, int>& element)     {         return element.second < 1;     }); }  //merge specified inventory current 1 //adding item quantaties if exist, creating new slot if don't void inventory::merge(inventory* inventory) {     // can't merge inventory itself!     if (inventory == this) return;      // loop through items added, , add them. our addition     // function take care of else     (auto : inventory->items)     {         this->add_item(it.first, it.second);     }     // same weapons     (auto : inventory->weapons)     {         this->add_weapon(it.first, it.second);     }     // same armour     (auto : inventory->armours)     {         this->add_armour(it.first, it.second);     }      return; }  void inventory::clear() {     this->items.clear();     this->weapons.clear();     this->armours.clear(); }  //output list of items stdout, nicely formatted int inventory::print_items(bool label = false) {     unsigned int = 1;      for(std::list<std::pair<item*, int>>::iterator =         this->items.begin(); != this->items.end(); ++it)     {         //number items if asked         if(label)         {             std::cout << i++ << ":";             //output item name, quantity , description             std::cout << it->first->name << " (" << it->second << ") - ";             std::cout <<it->first->description << std::endl;         }     }     //return number of items outputted, convienience     return this->items.size(); }  //output list of weapons stdout, nicely formatted int inventory::print_weapons(bool label = false) {     unsigned int = 1;      (auto : this->weapons)     {         if (label) std::cout << i++ << ": ";         std::cout << it.first->name << " (" << it.second << ") - ";         std::cout << it.first->description << std::endl;     }      return this->weapons.size(); }  //output list of armour stdout, nicely formatted int inventory::print_armours(bool label = false) {     unsigned int = 1;      for(std::list<std::pair<armour*, int>>::iterator =         this->armours.begin(); != this->armours.end(); ++it)     {         //number items if asked         if(label)         {             std::cout << i++ << ":";             //output item name, quantity , description             std::cout << it->first->name << " (" << it->second << ") - ";             std::cout <<it->first->description << std::endl;         }     }     //return number of items outputted, convienience     return this->armours.size(); }  //print entire inventory, unless empty void inventory::print(bool label = false) {     if(this->items.size() == 0 &&          this->weapons.size() == 0 &&         this->armours.size() == 0)     {         std::cout << "empty!" << std::endl;     }     else     {         this->print_items(label);         this->print_weapons(label);         this->print_armours(label);     }     return; } #endif //inventory_hpp 

main.cpp:

#include <iostream> #include <string> #include <vector> #include <list> #include <utility> #include <cstdlib> #include <ctime>  #include "area.hpp" #include "armour.hpp" #include "atlas.hpp" #include "creature.hpp" #include "dialogue.hpp" #include "inventory.hpp" #include "item.hpp" #include "weapon.hpp"  creature dialogue_newchar();  int main(void) {     std::vector<creature> creatureatlas;     std::vector<item> itematlas;     std::vector<weapon> weaponatlas;     std::vector<armour> armouratlas;     std::vector<area> areaatlas;      // build atlases     buildatlas_creature(creatureatlas);     buildatlas_item(itematlas);     buildatlas_weapon(weaponatlas);     buildatlas_armours(armouratlas);     buildatlas_area(areaatlas, itematlas, weaponatlas,         armouratlas, creatureatlas);      creature player;      //seed rand() system time, produce better random numbers     srand(time(null));      //main game menu dialogue     int result = dialogue("welcome jungle!", { "new game" }).activate();      switch (result)     {     case 1:         player = dialogue_newchar();         break;     default:         return 0;         break;     }     // set current area first area in atlas,     // placing player there upon game start     area* currentarea = &(areaatlas[0]);      // play game until function breaks loop , closes     while (1)     {         // if player has died inform them such , close         // program         if (player.health <= 0)         {             std::cout << "\t----you died----\n    game over\n";             return 0;         }          // activate current area's dialogue         result = currentarea->dialogue.activate();          // these moved inside of area code using         // event style system, allows less         // flexibility happens in each area. since we're         // defining areas in code anyway, sticking         // isn't of problem, , keeps things easy         // understand         if (currentarea == &(areaatlas[0]))         {             switch (result)             {             case 1:                 // move area 1                 currentarea = &(areaatlas[1]);                 break;             case 2:                 // search area                 currentarea->search(player);                 break;             default:                 break;             }         }         else if (currentarea == &(areaatlas[1]))         {             switch (result)             {                 // move area 0             case 1:                 currentarea = &(areaatlas[0]);                 break;                 // search area             case 2:                 currentarea->search(player);                 break;             default:                 break;             }         }     }      return 0;  }  //create new character creature dialogue_newchar() {     //ask name , class     std::cout << "choose name punk!" << std::endl;     std::string name;     std::cin >> name;      int result = dialogue("choose class!", { "fighter", "rogue" }).activate();      switch (result)     {     case 1:         //fighter favours health , strength         return creature(name, 35, 20, 10, 5, 10.0, 1, "fighter");         break;     case 2:         //rogue favours endurance , dexterity         return creature(name, 30, 5, 10, 20, 15.0, 1, "rogue");         break;     default:         //default should not happen, safe though!         return creature(name, 30, 10, 10, 10, 10.0, 1, "adventurer");         break;     } } 

i can add code additional files if needed. thanks!

you need move implementation of inventory class methods .cpp file.

what's happening header file included in few differnt .cpp files, thise methods compiled in several differnt compilation units. causes linking error says methids exist in differnt compilation unit.

in fact implementations except template implemtations should exist in own .cpp files, reason.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -