简体   繁体   English

多个定义的符号 C++ 错误

[英]Multiple Defined Symbols C++ error

I thought ifndef something #define something body #endif solved this error, so I'm not sure why this is happening.我认为ifndef something #define something body #endif解决了这个错误,所以我不确定为什么会这样。

//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__

Then there's two other cpp files that include "Library.h".然后还有另外两个包含“Library.h”的cpp文件。

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!问题在于,在 globals.h 头文件中,您声明了一组默认具有外部链接的变量:即所有全局变量!

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.对同一个函数有多个声明是完全合法的,这就是为什么如果几个不同的文件都#include相同的头并声明相同的函数,那就完全没问题了。 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.变量在 C++ 中只能定义一次(这称为一个定义规则),如果多个文件定义了相同的变量或函数,则会导致链接器错误,因为链接器不知道要使用哪个版本。 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.顺便说一下,这就是您没有#include .cpp 文件的原因,因为如果您这样做了,您将多次定义该标头导出的所有函数。

To fix this problem, in the header you'll want to change those variable definitions to variable declarations by using the extern keyword:要解决此问题,您需要在标头中使用extern关键字将这些变量定义更改为变量声明

//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 int ap;         //armour points
extern int wd;         //weapon damage
extern int lvl;        //level
extern int sp;         //skill points

This will allow any number of files to #include this header, since none of them are actually defining the variables;这将允许任意数量的文件#include此标头,因为它们实际上都没有定义变量; 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:然后,您应该创建一个新的 .cpp 文件,可能是 globals.cpp,它实际上定义了变量:

#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

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.这些是变量的实际定义,由于它们只存在于一个地方 (globals.cpp),因此您不会再遇到任何链接器错误。

Hope this helps!希望这可以帮助!

There are many issues with your C++ code你的 C++ 代码有很多问题

  • Never declare global variables directly in the header that is what is triggering the multiple symbols as they will appear in each compilation unit (~cpp file using them) One solution would be to make them extern but I would urge you to use a class or structure holding these parameters instead.切勿直接在触发多个符号的标头中声明全局变量,因为它们将出现在每个编译单元中(使用它们的 ~cpp 文件)一种解决方案是将它们设为 extern,但我建议您使用类或结构而是持有这些参数。

Beside this:除此之外:

  • Never do "using namespace xxx" inside header files on the global scope.永远不要在全局范围内的头文件中执行“使用命名空间 xxx”。 You will force everyone using the header to pull the symbols inside the global namespace.您将强制使用标头的每个人都将符号拉入全局命名空间中。
  • It is not following C++ semantics, looks more like C ( I would really wrap all player stats inside a class of the same name!)它不遵循 C++ 语义,看起来更像 C(我真的会将所有玩家统计信息包装在同名的类中!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM