   Multiple Source Files problem: SDL_Surface * already defined...
  I started my first multiple source file project today, a Breakout clone.I have 7 files:classes.hclasses.cppfunctions.hfunctions.cppglobals.hconstants.hmain.cppClasses.h declares classes and their member functions, classes.cpp defines those functions and functions.h/cpp works the same.Now, technically, the program wouldn't work anyway because I haven't done enough of it but I thought I should probably nip this one in the bud because it seems like a genuine problem...I have loads of SDL_Surface * objects declared in globals.hThese are used by functions.cpp, classes.cpp and indirectly by main.cpp.When I debug/run, I'm getting1>functions.obj : error LNK2005: "struct SDL_Surface * background" (?background@@3PAUSDL_Surface@@A) already defined in classes.objFor every surface. Classes.cpp has no surface declarations but of course it needs globals.h (which contains the declarations) to blit these surfaces to the screen from within the classes' member functions.Obviously, the compiler must be seeing classes.cpp with globals.h embedded in it first, and then sees it conflict in a later file when that file includes globals.h.So how do I fix this? Classes.cpp, functions.cpp etc. need to both include globals.h.I already have this in globals.h:#ifndef GLOBALS_H#define GLOBALS_H...<THE REST OF THE CODE>...#endifEDIT:Oh and can't you make arrays of class objects?I made a class Brick, and wrote: Brick bricks[100]; but it says it's missing a type specifier o_o   
  First let me say that this is a horrible way to divide your project. You should split it into files where each file contains stuff related to a specific aspect of your game. For example a brick.h/brick.cpp for you brick class.  Also, you should avoid globals where possible, and definitely not have "have loads of SDL_Surface * objects declared in globals.h".  You should really consider redesigning this.But on to your problem.  A variable such as an SDL_Surface* can only be defined in one translation unit (.cpp file).  Since you are (by including them), defining each surface in multiple translation units, you get link errors.  To fix this, you can declare it in many files, but define it in just one.To do this you put the following in your header:extern SDL_Surface* screen;Then, in exactly one source file:SDL_Surface* screen;This tells all of the files that include the header that this thing called screen is an SDL_Surface* and they should look for it somewhere else.As to your other question, that should be legal, I'm guessing that Brick is not defined at that point for some reason.<edit> Also, you may like the article Organizing  Code Files in C and C++.  It explains multiple files very well.   ;Quote:Original post by Simian ManFirst let me say that this is a horrible way to divide your project. You should split it into files where each file contains stuff related to a specific aspect of your game. For example a brick.h/brick.cpp for you brick class.  Also, you should avoid globals where possible, and definitely not have "have loads of SDL_Surface * objects declared in globals.h".  You should really consider redesigning this.Yes... I probably should ^_^I'll probably start over later...Quote:Original post by Simian ManTo do this you put the following in your header:extern SDL_Surface* screen;Then, in exactly one source file:SDL_Surface* screen;Aha... I knew about extern but I figured if I put it in globals.h that wouldn't make any sense... which it wouldn't. Never really thought about just defining it in a different file... d'oh.Quote:Original post by Simian ManAs to your other question, that should be legal, I'm guessing that Brick is not defined at that point for some reason.Yes... it's all a bit confusing. I really should restructure it.Quote:Original post by Simian Man<edit> Also, you may like the article Organizing  Code Files in C and C++.  It explains multiple files very well.Cool... I'll take a look at that. Thanks.   ;  Hmm... one more thing.If I have a class in a .cpp file, how do I properly declare it in the header?Right now I have the whole class X{function();etc.};in a header.Using extern class, extern is ignored, so that's obviously not how you do it.Surely if I have that in the header and the header is included in several files, it's going to error? I'm just setting my files up again so it's hard to verify very much, but if I #include my header with this class definition in 2 different files, it doesn't error for some reason. Is that just how you're supposed to do it?   ;Quote:Original post by Sean_SeanstonHmm... one more thing.If I have a class in a .cpp file, how do I properly declare it in the header?Right now I have the whole *** Source Snippet Removed ***in a header....Surely if I have that in the header and the header is included in several files, it's going to error?No; put the class body in the header like that; that's fine. C++ is designed to handle that.You can optionally (usually preferred, except for very simple functions that are unlikely to change) move the member functions to the .cpp file, by only putting their declaration within the class body. That looks like:.h file#ifndef FOO_H#define FOO_Hclass Foo {  void member_function(int arg);};#endif.cpp file#include "Foo.h"void Foo::member_function(int arg) {  // implementation}