2015-04-23 33 views
0

我試圖在我的空閒時間練習一些編碼(結合我的一些不同的興趣來幫助保持自己的參與),並且遇到了一個奇怪的錯誤,我可以找不到答案。我有4個我正在使用的文件,兩個頭文件,一個類定義文件和一個主文件。我相當有信心,我沒有多次包含Dice.h文件(但是這是錯誤指向的地方,因此我不再確定這個問題)。我在這裏弄錯了什麼來產生這些錯誤?在C++中的多重定義(Visual Basic 2010)

錯誤代碼

錯誤3錯誤LNK1169:一個或一個以上乘法定義的符號實測值(文件路徑修整)

錯誤2錯誤LNK2005: 「INT __cdecl骰子(INT,INT)」( (文件路徑修剪)

filepath:c:\ Users \ Username \ documents \ visual studio2010 \ Projects \ RPGTest \ RPGTest \ RPGTest。(引用錯誤3一個.exe文件,錯誤2引用.obj文件)。

代碼本身:

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; 
do { 
    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 a name for this creature: "; 
    cout << "\nSorry! I don't work yet!"; 
    cout << "\nInstead all creatures are 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 to RPG Creature Tester v0.1\n"; 
    cout << "This .exe file is meant to test the creature class functions and definitions.\n"; 
    cout << "This will be done by randomly generating and displaying a creature.\n"; 
    cout << "What you don't see right now is the random generation of a creature.\n"; 
    cout << "Once it's finished, the \'statsheet\' will be 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; 
} 

由於我主要是自學成才的,我很高興其他任何意見,但我的主要問題是,我不知道爲什麼,我發現了「多」的定義錯誤。我對其他有類似錯誤信息的問題做了一些研究,但是我沒有看到任何立即跳出來的東西作爲「答案」。

謝謝大家!

回答

3

C++的工作原理是編譯單個翻譯單元,然後將它們鏈接在一起。

這意味着每個源文件都會自行編譯。由於#include指令基本上插入所有包含的代碼,在你的情況,你最終具有定義

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

編譯經過精細,但鏈接時,這個功能的多個定義被發現所以這產生了多個翻譯單位錯誤。

爲了解決這個問題,有兩種方式:

  • 聲明在頭文件int dice(int, int)但定義(實現它)在源文件中
  • 保持的定義,因爲它是,但前面加上static它。這告訴編譯器,每個翻譯單元將獲得它自己的dice方法。此解決方案雖然誘人,但會導致二進制大小增加,因爲您將有多次執行相同的方法
+0

static? '內聯'不是一個(更好的)解決方案嗎?老實說,我甚至都不知道函數的靜態作品。 –