c++ - C++ 中的多重定义 (Visual Basic 2010)

标签 c++ visual-studio-2010 multiple-definition-error

我试图在空闲时间练习一些编码(结合我的许多不同兴趣来帮助自己保持专注),但我遇到了一个我找不到答案的奇怪错误。我有 4 个正在使用的文件,两个头文件,一个类定义文件和一个主文件。我相当有信心我不会多次包含 Dice.h 文件(但是这是错误指向的地方,我不确定了,因此这个问题)。我在这里搞砸了什么导致这些错误?

错误代码

Error 3 error LNK1169: one or more multiply defined symbols found (file path trimmed)

Error 2 error LNK2005: "int __cdecl dice(int,int)" (?dice@@YAHHH@Z) already defined in Creature.obj (file path trimmed)

The filepath: c:\Users\Username\documents\visual studio2010\Projects\RPGTest\RPGTest\RPGTest.(error 3 referenced a .exe file, error 2 referenced a .obj file).

代码本身:

骰子.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

生物.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

生物.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;
}

因为我主要是自学成才,所以我很乐意听取任何其他建议,但我的主要问题是我不确定为什么会出现“多重”定义错误。我对具有类似错误消息的其他问题进行了一些研究,但我没有看到任何立即跳出来作为“答案”的东西。

谢谢大家!

最佳答案

C++ 的工作原理是编译单个翻译单元,然后将它们链接在一起。

这意味着每个源文件都是自己编译的。由于 #include 指令基本上插入了所有包含的代码,因此在您的情况下,您最终会拥有多个定义的翻译单元

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

编译顺利,但在链接时,发现此函数的多个定义,因此会产生错误。

解决这个问题有两种方法:

  • 在头文件中声明 int dice(int, int) 但在源文件中定义(实现)
  • 保持定义不变,但在其前面加上static。这告诉编译器每个翻译单元将获得自己的 dice 方法。这个解决方案虽然很诱人,但会导致二进制文件大小增加,因为您将对同一方法进行多次实现

关于c++ - C++ 中的多重定义 (Visual Basic 2010),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29815123/

相关文章:

c++ - 在 VS 2010 中链接 WASAPI

visual-studio-2010 - MbUnit 和 Resharper 集成在 Visual Studio 2010 中不起作用

c++ - vector 多重定义链接错误

c++ - 如何在一个表达式中生成并返回结果?

c++ - OpenGL/C++ 将所有纹理 Assets 作为静态成员存储在单独的类中

visual-studio-2010 - 预览 : "The size necessary to buffer the XML content exceeded the buffer quota" hides original error 上的 SSRS 错误

c++ - 为什么这不会将运算符重载标记为内联导致重复定义错误?

c++ - 如何将对象的方法传递给外部函数

c++ - 如何用STL算法代替for循环?

c++ - 如何在对象库中部分公开对象内容?