SyxDuLappen
SyxDuLappen

Reputation: 59

Redefinition Error after moving code into another Header

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;

Upvotes: 1

Views: 149

Answers (2)

NathanOliver
NathanOliver

Reputation: 180998

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

Upvotes: 2

Vtik
Vtik

Reputation: 3140

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

Upvotes: 0

Related Questions