0

I know this is a common problem, but I am pretty sure there is no error with how I include the files.

I'll give you the basic files.

Main.cpp:

#include "GameState.h"
#inlcude "Timer.h"

int main ( int argc, char** argv ) {

GameState.h:

#pragma once
#include "Character.h"

Character.h:

#pragma once
#include "Setup.h"

Setup.h:

#pragma once

#include "SDL.h"
#include "SDL_main.h"
#include "SDL_image.h"

Error report:

Error   1   error LNK2005: "void __cdecl apply_surface(int,int,struct SDL_Surface *,struct SDL_Surface *,struct SDL_Rect *)" (?apply_surface@@YAXHHPAUSDL_Surface@@0PAUSDL_Rect@@@Z) already defined in Character.obj   C:\Users\Jim\Documents\C++\herorpg\herorpg\Main.obj
Error   2   error LNK2005: "bool __cdecl init(struct SDL_Surface * &)" (?init@@YA_NAAPAUSDL_Surface@@@Z) already defined in Character.obj   C:\Users\Jim\Documents\C++\herorpg\herorpg\Main.obj
Error   3   error LNK2005: "bool __cdecl load_files(struct SDL_Surface * * const)" (?load_files@@YA_NQAPAUSDL_Surface@@@Z) already defined in Character.obj C:\Users\Jim\Documents\C++\herorpg\herorpg\Main.obj
Error   4   error LNK2005: "struct SDL_Surface * __cdecl load_image(char *)" (?load_image@@YAPAUSDL_Surface@@PAD@Z) already defined in Character.obj    C:\Users\Jim\Documents\C++\herorpg\herorpg\Main.obj
Error   6   error LNK1169: one or more multiply defined symbols found   C:\Users\Jim\Documents\C++\herorpg\Debug\herorpg.exe

Is there anything wrong with what I'm including? If you think more information is required, I'll post the full code. Just seemed like a nuisance before.

James Hurley
  • 833
  • 1
  • 10
  • 33

3 Answers3

2

C++ has a rule called the one definition rule. Among other things, this rule specifies that there cannot be multiple definitions of a function across your program. You can't have two translation units that both define a function, otherwise you break this rule. You can think of a translation unit as a .cpp file with all of its headers included into the appropriate places.

So if you have a header file, foo.h, that looks like this:

#ifndef FOO_H
#define FOO_H

int foo() { return 5; }

#endif

And then you include this header in two or more .cpp files, each of the translation units will have their own definition. This violates the one definition rule.

To fix this, your header should give a function declaration like so:

#ifndef FOO_H
#define FOO_H

int foo();

#endif

Then, in the corresponding foo.cpp file, give a definition for the function:

#include "foo.h"

int foo() { return 5; }

This means that only the foo.cpp translation unit will have a definition of foo. Any uses of foo in other translation units will be linked to that definition.

An alternative is to declare the function as inline, as in:

#ifndef FOO_H
#define FOO_H

inline int foo() { return 5; }

#endif

The reason this is allowed is because each translation must be able to see the definition of such a function to be able to inline it. However, I don't recommend using inline all willy-nilly.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
0

Linker errors are not caused by #include errors. Linker errors usually happens when the compilers can't find the definition of something. Or if it finds multiple definitions ( such as in this case )

Check if you are linking with multiple SDL libraries or if you have defined the functions yourself somwhere in the code

olevegard
  • 5,294
  • 1
  • 25
  • 29
0

possible reason:

  1. define functions in header files. functions should only be defined in .cpp file.

  2. looped header files including. like: a.h includes b.h, b.h includes c.h, and c.h includes a.h. Sometime the looped including is not obvious, but it does happen. "#pragma once" can only prevents one header file from being included more than once, but cannot prevent looped including. To solve this problem, using "forward declaration" to replace some #include statements.

some links about forward declaration:

http://en.wikipedia.org/wiki/Forward_declaration

When can I use a forward declaration?

http://msdn.microsoft.com/en-us/library/f432x8c6(v=vs.80).aspx

Community
  • 1
  • 1
skyfree
  • 867
  • 2
  • 10
  • 29