Error: redefinition of 'class SpriteSheet'

Hi guys,

I have one rather strange compilation problem up my sleaves (error: redefinition of ‘class SpriteSheet’)…

I have a class named ‘SpriteSheet’ which is declared in a header file like so:

#include <SDL/SDL.h>
#include <string>

using namespace std;

class SpriteSheet  {
        SDL_Surface *surface;

        int number_rows;
        int number_cols;

        SDL_Rect *bounds_array;
        SpriteSheet(string, int, int);
        // Deconstructor needed to delete dynamic stuff
        SDL_Surface* get_SpriteSheet();
        SDL_Rect* get_bounds(int);

and a inplymentation CPP file:

#include <SDL/SDL.h>
#include <string>
#include "SDL_Utilities.h"
#include "SpriteSheet.h"

using namespace std;

SpriteSheet::SpriteSheet(string filename, int rows, int cols)  {
    surface = load_image(filename.c_str());

    bounds_array = new SDL_Rect[cols * rows];

    int width_of_single_frame = surface->w / cols;
    int height_of_single_frame = surface->h / rows;

    for (int y = 0; y < number_rows; y++)  {
        for (int x = 0; x < number_cols; x++)  {
           SDL_Rect element_bounds;

           element_bounds.x = x * width_of_siingle_frame;
           element_bounds.y = y * height_of_single_frame;
           element_bounds.w = width_of_single_frame;
           element_bounds.h = height_of_single_frame;

           bounds_array[y * number_rows + x] = element_bounds;
    number_rows = rows;
    number_cols = cols;

SpriteSheet::~SpriteSheet()  {    
    delete bounds_array[i];

SDL_Surface* SpriteSheet::get_SpriteSheet()  {
    return surface;

SDL_Rect* SpriteSheet::get_bounds(int x)  {
    // Temp
    int y = 0;

    return &bounds_array[y * number_rows + x];

I have checked everywhere with the find funcction in the IDE and i cannot find any other definitions of the class, so i don`t know what the compiler is talking about?

Thanks, PraiseWheat

Another place to help out with this kind of issue would be or Idk which compiler you’re using, but I notice you have 2 header files you’re including. If you “cross” header files because this includes that which includes the other thing which includes something else which includes the first one, then you can get problems like that.

“What I do is just have each file include only one header, and I have a total of 6 headers and 7 .cpps (and almost 7000 lines). Each .cpp only includes one header, and then the main.cpp file includes one of the headers, so I just make sure that it was in the right order so everything works by the .cpps including things that they use from other .cpps (via the header). That’s probably not that perfect, but it works; I’m actually relatively new to c++ with a c background so I have a ways to go before I get mistaken for Tim Jones.”

Edit 5-10-12: The above response in quotations is officially deprecated, but I left it here because PraiseWheat responded to it below. Anyone reading this, please follow the advice of the Naval officer three posts down.

Sorry, I don`t understand. Do you mean make sure you are only include one unique header file in the whole of the project? And if so how would you do it? With the preprocessor? But how?


Edit 5-10-12: Well I deleted by original response because it was just too embarrassing; I wonder how I ever got my game good enough for The Software Labs back in the '80s with practices like that. The Naval officer below knows what’s up, and I have for awhile too, by now.

at the top of the file:


at the bottom of the file:


Thanks very much guys, It works!!