1

so i'm having a problem: I cutted out some coding(structs) and pasted it in a new Header-file. I included the new Header-file everywhere it is needed, so it shouldnt get me an error, but after i tried building my dll file, i got tons of errors saying that those structs i cutted out/pasted are redefinitions. I clicked on some of those "redefinitions" and the "originals" and i got to the same struct the same time, meaning there is only one of them so it cant be a redefinition. I'm so confused at the moment and i would really appreciate some help! Thanks in advance :)

EDIT: i moved this:

struct Game_s
{
    bool loaded;
    bool FirstUser;
    bool AlwaysVerfied;
    bool DoingUnlockAll;
    int Globaltimer;
    int MaxUnlockAll;
    time_t t;
};
Game_s Game;

from a Header file called MW2Class.h to another class called Structs.h, looking like this:

#include "stdafx.h"
#include <stdio.h>
#include <string>
#include <xbox.h>

struct Game_s
{
    bool loaded;
    bool FirstUser;
    bool AlwaysVerfied;
    bool DoingUnlockAll;
    int Globaltimer;
    int MaxUnlockAll;
    time_t t;
};
Game_s Game;
SyxDuLappen
  • 59
  • 2
  • 8
  • 1
    Please post a [mcve] – NathanOliver May 20 '16 at 14:46
  • There's nothing can be said for sure until you paste your code that you have problems with. But first assumption would be to check if you have include guard or `#pragma once` in your header file. – Teivaz May 20 '16 at 14:48
  • Are you using precompiled headers? – Viktor Liehr May 20 '16 at 14:48
  • i added an example i didnt added all of it because there are around 500 structs like this. i use #pragma region structs at the beginning and #pragma endregion at the end of all the structs... – SyxDuLappen May 20 '16 at 14:51
  • Are you sure it wasn't a link error regarding the variable `Game`? – molbdnilo May 20 '16 at 14:55
  • @SyxDuLappen #pragma (region & endregion) are just for better displaying the code (mainly folding), nothing more. Here's an [explanation](https://msdn.microsoft.com/en-us/library/td6a5x4s.aspx) – Vtik May 20 '16 at 14:58

2 Answers2

2

Your issue is you have a global variable declared in your header file

Game_s Game;

Is going to add Game to every translation unit you include the header in(include guards do not stop this). When you get to the linking stage the linker will see all of theses global variables that are the same and will not know what to do with them.

If you really want the global variable you will need to declare it with extern and define it in one translation unit. For more information on that see: Global variables in header file

Community
  • 1
  • 1
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
0

Make sure to add the inclusion guard to your headers :

Suppose your header is test_file.hpp, the include guards will be :

#ifndef TESTFILE_HPP_
#define TESTFILE_HPP_
#endif

That prevents multiple inclusions of your header.

Edit :

1 - in your case

#ifndef STRUCTS_H_
#define STRUCTS_H_
#endif
Vtik
  • 3,073
  • 2
  • 23
  • 38