Multiple Definitions in C++ (Visual Basic 2010) -


i'm attempting practice coding in free time (combining number of different interests of mine keep myself engaged) , i've encountered odd error can't find answer to. have 4 files i'm working with, 2 header files, 1 class definition file , main file. i'm confident i'm not including dice.h file more once (however error points , i'm not sure anymore, hence question). have bungled here produce these errors?

the error codes

error 3 error lnk1169: 1 or more multiply defined symbols found (file path trimmed)

error 2 error lnk2005: "int __cdecl dice(int,int)" (?dice@@yahhh@z) defined in creature.obj (file path trimmed)

the filepath: c:\users\username\documents\visual studio2010\projects\rpgtest\rpgtest\rpgtest.(error 3 referenced .exe file, error 2 referenced .obj file).

the code itself:

dice.h

#ifndef set_dice_h_ #define set_dice_h_  #include <iomanip> #include <iostream> #include <stdlib.h>  using namespace std;  int dice(int number, int sides){  int total=0, dice; srand(time(null)); int results=0; {     dice = rand()%sides+1;     total+=dice;     number--; } while (number > 0); results = total; return results; } #endif 

creature.h

#ifndef creature_h_ #define creature_h_  #include <iomanip> #include <iostream> #include "dice.h" using namespace std;  class creature { public:     creature(int,int,int,int,int,int,int,int,int,int,int,int);     void set_hp();     void set_saves();     void set_ac();     void set_bab();     void set_name();     void update_hp(int);     void update_ac(int);     void update_fsave(int);     void update_rsave(int);     void update_wsave(int);     int get_ac();     int get_hp();     int get_fsave();     int get_rsave();     int get_wsave();     int get_bonus(int);     int get_bab();     string get_name(); private:     int strength, dexterity, constitution, intellegence, wisdom, charisma;     int bab, fbsave, rbsave, wbsave;     int hdnum, hdsize;     int hp, fsave, rsave, wsave, ac;     string name;  }; #endif 

creature.cpp

#include "creature.h" #include <math.h> #include <iostream>  using namespace std;  creature::creature(int strength,int dexterity,int constitution,     int intellegence,int wisdom,int charisma,int bab,int fbsave,     int rbsave,int wbsave,int hdnum,int hdsize){         strength = strength;         dexterity = dexterity;         constitution = constitution;         intellegence = intellegence;         wisdom = wisdom;         charisma = charisma;         bab = bab;         fbsave = fbsave;         rbsave = rbsave;         wbsave = wbsave;         hdnum = hdnum;         hdsize = hdsize; }  int creature::get_bonus(int stat){     int bonus = floor((double(stat)-10)/2);     return bonus; }  void creature::set_ac(){     ac=10+get_bonus(dexterity); }  void creature::set_hp(){     hp = dice(hdnum,hdsize) + get_bonus(constitution)*hdnum; }  void creature::set_saves(){     fsave = fbsave + get_bonus(constitution);     rsave = rbsave + get_bonus(dexterity);     wsave = wbsave + get_bonus(wisdom); }  void creature::set_bab(){     bab = hdnum; }  void creature::set_name(){     cout << "please enter name creature: ";     cout << "\nsorry! don't work yet!";     cout << "\ninstead creatures named larry!\n";     name = "larry!"; }  void creature::update_hp(int input){     hp = hp + input; }  void creature::update_fsave(int input){     fsave = fsave+input; }  void creature::update_rsave(int input){     rsave = rsave+input; }  void creature::update_wsave(int input){     wsave = wsave+input; }  void creature::update_ac(int input){     ac = ac+input; }  int creature::get_ac(){     return ac; }  int creature::get_hp(){     return hp; }  int creature::get_fsave(){     return fsave; }  int creature::get_rsave(){     return rsave; }  int creature::get_wsave(){     return wsave; }  int creature::get_bab(){     return bab; } 

rpgtest.cpp

#include "creature.h" #include <math.h> //#include "dice.h" #include <iostream> #include <iomanip>  using namespace std;  int main(){      int str = dice(3,6), dex = dice(3,6), con = dice(3,6), intel = dice(3,6), wis = dice(3,6), cha = dice(3,6);     int fbs = dice(1,6), rbs = dice(1,6), wbs = dice(1,6);     int hdn = dice(1,10), hds = 8, bab = dice(1,8);      cout << "welcome rpg creature tester v0.1\n";     cout << "this .exe file meant test creature class functions , definitions.\n";     cout << "this done randomly generating , displaying creature.\n";     cout << "what don't see right random generation of creature.\n";     cout << "once it's finished, \'statsheet\' shown.\n";     cout << "cheers!\n\n";      creature potato (str, dex, con, intel, wis, cha, bab, fbs, rbs, wbs, hdn, hds);      potato.set_ac();     potato.set_hp();     potato.set_name();     potato.set_saves();      cout << "output brick yay\n";     cout << "str: " << str << endl;     cout << "hp: " << potato.get_hp() << " ac: " << potato.get_ac() << " fort/reflex/will save: " << potato.get_fsave() << "/" << potato.get_rsave() << "/" << potato.get_wsave();      return 0; } 

since i'm self-taught i'm happy other advice main issue i'm not sure why i'm getting "multiple" definition error. did research other questions similar error messages didn't see jumped out @ me "the answer".

thanks all!

c++ works compiling single translation units , linking them together.

this means each source file gets compiled on own. since #include directive inserts code included, in situation end having multiple translation units define

int dice(int number, int sides) {   ... } 

compilation goes through fine but, when linking, multiple definition of function found generates error.

to solve problem have 2 ways:

  • declare int dice(int, int) in header file define (implement it) in source file
  • keep definition prepend static it. tells compiler each translation unit own dice method. solution, although tempting, leads binary size increase since have multiple implementation of same method

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -