Multiple defined characters C ++ error

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__ 

Then there are two more cpp files that include "Library.h".

+4
source share
2 answers

The problem is that in your globals.h header file, you declare a set of variables that have an external link by default: namely, all global variables!

When you prototype a function in the header file, you declare the function, but do not define it. It is completely legal to have several declarations of the same function, so if several different files all #include the same header and declare the same function, this is completely normal. On the other hand, if you have global variables in the header file, you define these variables. Variables can only be defined once in C ++ (this is called a rule of one definition), and if several files define the same variable or function, this will cause a linker error because the linker will not know which version to use. By the way, by the way, you are not making #include .cpp files, since if you did, you would multiply all the functions exported by this header.

To fix this problem, in the header you will want to change these variable definitions to variable declarations 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 

This will allow you to use any number of files for #include this header, since none of them defines variable names; they simply declare that the variables will exist somewhere. Then you should create a new .cpp file, possibly globals.cpp, which 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 

These are actual definitions of variables, and since they exist in only one place (globals.cpp), you will not get more linker errors.

Hope this helps!

+12
source

There are many problems with your C ++ code

  • Never declare global variables directly in the header, this is what triggers several characters, since they will be displayed in each compilation unit (~ cpp file with their use). One solution would be to make them extern, but I would strongly recommend that you use a class or structure containing these parameters.

Besides:

  • Never do โ€œuse the xxx namespaceโ€ inside header files in the global scope. You will force everyone using the header to pull out characters inside the global namespace.
  • This does not correspond to the semantics of C ++, it is more like C (I would really wrap all the statistics of the players inside the same name!)
+3
source

Source: https://habr.com/ru/post/1344482/


All Articles