多个定义符号C ++错误 [英] Multiple Defined Symbols C++ error

查看:153
本文介绍了多个定义符号C ++错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为 ifndef的东西#define something body #endif 解决了这个错误,所以我不知道为什么会发生这种情况。

I thought ifndef something #define something body #endif solved this error, so I'm not sure why this is happening.

//Library.h
#ifndef __LIBRARY__
#define __LIBRARY__

#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cstdarg>
#include <vector>
#include <ctime>
#include <cmath>
#include <cstdlib>

//file includes
#include "Globals.h"

using namespace std;

#endif //__LIBRARY__

-

//globals.h
//global variables
#ifndef __GLOBAL__
#define __GLOBAL__

#include <vector>
#include <iostream>
#include <string>

//prototypes
bool Poglathon(std::vector<std::string>& text);
void NPCTalk(std::string const& speaker,std::vector<std::string> const& text);
void wait(double seconds);

//player stats
std::string name;
double str;     //strength
double wis;     //wisdom
double ref;     //reflex
double hp;      //health points
double i;       //initiative
double inte;    //intelligence
double c;       //courage
int gold;       //gold
int xp;         //experience
int ap;         //armour points
int wd;         //weapon damage
int lvl;        //level
int sp;         //skill points

#endif //__GLOBAL__

cpp文件包含Library.h。

Then there's two other cpp files that include "Library.h".

推荐答案

问题是,在你的globals.h头文件,声明一组变量,默认情况下具有外部链接:即所有全局变量!

The problem is that in your globals.h header file, you're declaring a suite of variables that by default have external linkage: namely, all the globals!

当你在头文件中对函数进行原型化时, / em>一个函数,但不能定义。有相同的函数的多个声明是完全合法的,这就是为什么如果几个不同的文件所有 #include 同一个头并声明相同的功能,它是完美的。另一方面,如果在头文件中有全局变量,则定义这些变量。变量只能在C ++中定义一次(这被称为<一>定义规则),如果多个文件定义相同的变量或函数,它将导致链接器错误,因为链接器不知道版本使用。这是为什么,你不 #include .cpp文件,因为如果你做了,你会多次定义所有的功能导出的标题。

When you prototype a function in a header file, you are declaring a function, but not defining it. It's perfectly legal to have multiple declarations of the same function, which is why if several different files all #include the same header and declare the same function it's perfectly fine. On the other hand, if you have global variables in a header file, you are defining those variables. Variables can only be defined once in C++ (this is called the one-definition rule), and if multiple files define the same variable or function it will cause a linker error because the linker won't know which version to use. This is the reason, by the way, that you don't #include .cpp files, since if you did you'd multiply define all the functions exported by that header.

要解决此问题,您需要在标题中将这些变量定义更改为变量声明使用 extern 关键字:

To fix this problem, in the header you'll want to change those variable definitions to variable declarations by using the extern keyword:

//player stats
extern std::string name;
extern double str;     //strength
extern double wis;     //wisdom
extern double ref;     //reflex
extern double hp;      //health points
extern double i;       //initiative
extern double inte;    //intelligence
extern double c;       //courage
extern int gold;       //gold
extern int xp;         //experience
extern     extern int ap;         //armour points
extern int wd;         //weapon damage
extern int lvl;        //level
extern int sp;         //skill points

这将允许任何数量的文件 #include 这个头,因为它们都没有真正定义变量;他们只是声明变量将存在于某处。然后,你应该创建一个新的.cpp文件,可能是globals.cpp,它实际上定义了变量:

This will allow any number of files to #include this header, since none of them are actually defining the variables; they're just declaring that the variables will exist somewhere. Then, you should create a new .cpp file, probably globals.cpp, that actually defines the variables:

#include "globals.h"

std::string name;
double str;     //strength
double wis;     //wisdom
double ref;     //reflex
double hp;      //health points
double i;       //initiative
double inte;    //intelligence
double c;       //courage
int gold;       //gold
int xp;         //experience
int ap;         //armour points
int wd;         //weapon damage
int lvl;        //level
int sp;         //skill points



这些是变量的实际定义,因为它们只存在于一个地方(globals.cpp),你将不会得到任何更多的链接器错误。

These are the actual definitions for the variables, and since they exist in just one place (globals.cpp) you won't get any more linker errors.

希望这有助于!

这篇关于多个定义符号C ++错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆