Another c++ error

Started by
27 comments, last by Zahlman 12 years, 6 months ago
Also I cleared about 50 of the original 299 error messages all because I had the pointer on the wrong side of 3 SDL surfaces
Advertisement
If the files and errors have changed you will need to repost them. Line 21 of main.h is empty space.
Also don’t name things “even”. It is likely to conflict with something.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Anyone else thinking this needs a psychic debugger?

I don't know if it's possible, or if you even want to, but maybe you could zip up your whole project and post it? It seems like this back-and-forth exchange isn't going anywhere at all.
What is the reason behind your tile_load.h including main.h? I don't see a reason for it to be included. Also, your main.h and main.cpp both include tile_load.h. Not saying that's going to be a miracle fix, but if your include guards or #pragma once...es aren't doing their job, you're going to get cyclical inclusion.
The inclusion of main.h could very well be the issue. I, having nothing better to do :P, threw together a minimal example project and, if I include main.h in my tile.h, I get the same errors as you...
main.h(7): error C2143: syntax error : missing ';' before '*'
main.h(7): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
main.h(7): error C2065: 'TOTAL_TILES' : undeclared identifier
main.h(7): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int


Here are the files, if you want to test it out.

tile.h#ifndef TILE_H
#define TILE_H

#include <iostream>
//#include "main.h"

const int TOTAL_TILES = 225;

class Tile
{
public:
Tile(const std::string name);
~Tile();
std::string GetName();
private:
std::string name_;
};

#endif // TILE_H

tile.cpp#include "tile.h"

Tile::Tile(const std::string name) : name_(name)
{
//std::cout << name_.c_str() << std::endl;
}
Tile::~Tile()
{
}
std::string Tile::GetName()
{
return name_;
}

main.h#ifndef MAIN_H
#define MAIN_H

#include <iostream>
#include "tile.h"

extern Tile *tiles[TOTAL_TILES];

#endif // MAIN_H

main.cpp#include "main.h"

Tile *tiles[TOTAL_TILES];

int main()
{
tiles[0] = new Tile("One");
std::cout << tiles[0]->GetName().c_str();

delete tiles[0];


// quick and dirty way to keep the cmd window open.
int i;
std::cin >> i;

return 0;
}

Compile it and test, then un-comment the //#include "main.h" in tile.h and re-compile it.
Average midget Thank you, I did get your point that it would just keep going around and around making nearly unlimited errors. My error count has gone back up to 299 but it is now slowly declining.
You have a circular header dependency, as AverageMidget points out. #include is just dumb text replacement. The preprocessor literally copies and pastes the preprocessed contents of #included files at the point where the include is encountered.

Let us take a simpler example to illustrate. We have the following files:

tile.h

#ifndef TILE_H
#define TILE_H

class Tile
{
public:
enum Type
{
Green,
Black
// More tile types...
};

Tile(int x, int y, Type type)
:
x(x),
y(y),
type(type)
{
}

// Useful member functions...

private:
int x;
int y;
Type type;
};

#endif


tile_load.h

#ifndef TILE_LOAD_H
#define TILE_LOAD_H

#include "tile.h"
#include "main.h"

const int TILE_WIDTH = 32;
const int TOTAL_TILES = 225;
const int TILE_HEIGHT = 32;
const int TILE_SPRITES = 2;

bool set_tiles(Tile *tiles[]);

#endif


main.h

#ifndef MAIN_H
#define MAIN_H

#include "tile_load.h"

const int level_width = 15;
const int level_height = 15;

extern Tile *tiles[TOTAL_TILES];

#endif


Now imagine our main function source file:

#include "main.h"

int main()
{
set_tiles(tiles);
}

Let us look at what the tools do when compiling main.cpp. First, the file must be preprocessed to create a translation unit.

Here is our translation unit, annotated with comments to explain the preprocessor logic:

// Preprocessor begin include main.h
// Preprocessor check if MAIN_H defined: no
// Preprocessor add MAIN_H to list of defines (currently none)
// Preprocessor begin include tile_load.h
// Preprocessor check if TILE_LOAD_H defined: no
// Preprocessor add TILE_LOAD_H to list of defines (currently MAIN_H)
// Preprocessor begin include TILE_H
// Preprocessor check if TILE_H defined: no
// Preprocessor add TILE_H to list of defines (currently MAIN_H, TILE_LOAD_H)

class Tile
{
public:
enum Type
{
Green,
Black
// More tile types...
};

Tile(int x, int y, Type type)
:
x(x),
y(y),
type(type)
{
}

// Useful member functions...

private:
int x;
int y;
Type type;
};

// Preprocessor end include TILE_H
// Preprocessor begin include main.h
// Preprocessor check if MAIN_H defined: yes
// Preprocessor end include main.h

const int TILE_WIDTH = 32;
const int TOTAL_TILES = 225;
const int TILE_HEIGHT = 32;
const int TILE_SPRITES = 2;

bool set_tiles(Tile *tiles[]);

// Preprocessor end include tile_load.h


const int level_width = 15;
const int level_height = 15;

extern Tile *tiles[TOTAL_TILES];

// Preprocessor end include main.h

int main()
{
set_tiles(tiles);
}

This file is what the compiler actually sees. It sees one monolithic file which it compiles on its own. The file is compiled top to bottom (more or less), so if the order of the declarations that end up being included is incorrect then the compiler will issue errors. Your compiler will have a flag somewhere where you can ask it to output the preprocessed file, if you'd like to see for yourself. It will be scary experience though if you ever look at a preprocessed file that includes the standard library, Windows.h, SDL.h or any other large header.

You might already see that we just narrowly avoided trouble. When tile_load.h tried to include main.h, nothing happened because MAIN_H was already defined. Luckily in this simple example nothing in tile_load.h depended on anything that was only visible in main.h.

You're running into trouble because your more complex files do depend on each other's contents.

To catch such problems early, for each header file, have a source file which includes that header as the first line. This ensures that each header works in isolation. Another piece of advice is to avoid "pull everything in" headers like "main.h", "global.h", etc. Each header and source file should include only what it needs.

If you have some common external headers, from the Standard C++ Library, SDL or Boost, having a "common.h" or similar isn't too bad because these headers should never cause circular dependencies. However you will often be pulling in many unnecessary headers, which will affect your build times (note in my example above that "tile.h" doesn't need to include anything).

In a small project your build times will usually be so short that it would be difficult to measurably affect them, I wouldn't worry about them. When it does become an issue, moving these external headers to a precompiled header is one way to solve this.
Thanks so much guys my error count has gone down to 47 but hopefully thats nothing google can't fix.
Ok I've fixed all the compiler errors but now I'm getting linker errors. I'm completely confused its telling me that the variable clips is already defined in basic_functions.obj and class_player.obj but in both basic function files and both class_player files I haven't declared clips. here is my error messages. There are other errors but the other 4 are all related to "clips" and basic functions. This time it didn't come with a line number.


Error 2 error LNK2005: "struct SDL_Rect * clips" (?clips@@3PAUSDL_Rect@@A) already defined in basic_functions.obj class_player.obj


class_player.cpp
#include "SDL.h"
#include "graphic_functions.h"
#include "graphics.h"
#include "class_varibles.h"
#include "class_tile.h"
#include "tile_load.h"
#include "class_player.h"
#include "main.h"

// the camera
SDL_Rect camera = {0, 0, screen_width, screen_height};

player::player()
{
//Initialize the offsets
box.x = 0;
box.y = 0;
box.w = player_width;
box.h = player_height;

//Initialize the velocity
xvel = 0;
yvel = 0;
}

void player::handle_input(){

//if a key was pressed
if(event.type == SDL_KEYDOWN){

// adjust the velocity
switch(event.key.keysym.sym){

case SDLK_UP: yvel -= player_height / 2; break;
case SDLK_DOWN: yvel += player_height / 2; break;
case SDLK_LEFT: xvel -= player_width / 2; break;
case SDLK_RIGHT: xvel += player_width / 2; break;

}
}

//if a key was released
else if(event.type == SDL_KEYUP){

//adjust the velocity
switch(event.key.keysym.sym){

case SDLK_UP: yvel += player_height / 2; break;
case SDLK_DOWN: yvel -= player_height / 2; break;
case SDLK_LEFT: xvel += player_width / 2; break;
case SDLK_RIGHT: xvel -= player_width /2; break;

}

}
}

void player::move(Tile *tiles[])
{
//Move the dot left or right
box.x += xvel;

//If the dot went too far to the left or right or touched a wall
if( ( box.x < 0 ) || ( box.x + player_width > level_width ) || touches_wall( box, tiles ) )
{
//move back
box.x -= xvel;
}

//Move the dot up or down
box.y += yvel;

//If the dot went too far up or down or touched a wall
if( ( box.y < 0 ) || ( box.y + player_height > level_height ) || touches_wall( box, tiles ) )
{
//move back
box.y -= yvel;
}
}

void player::set_camera()
{
//Center the camera over the player
camera.x = ( box.x + player_width / 2 ) - screen_height / 2;
camera.y = ( box.y + player_height / 2 ) - screen_height / 2;

//Keep the camera in bounds.
if( camera.x < 0 )
{
camera.x = 0;
}
if( camera.y < 0 )
{
camera.y = 0;
}
if( camera.x > level_width - camera.w )
{
camera.x = level_width - camera.w;
}
if( camera.y > level_height - camera.h )
{
camera.y = level_height - camera.h;
}
}

void player::show(){

// shows the player
apply_surface( box.x - camera.x, box.y - camera.y, surface_player, surface_screen, NULL);
SDL_SetColorKey(surface_player, SDL_SRCCOLORKEY, SDL_MapRGB(surface_player->format, 200, 0, 200));

}


class_player.h
#ifndef CLASS_PLAYER_H
#define CLAS_PLAYER_H
// includes
#include "SDL.h"
#include "graphic_functions.h"
#include "graphics.h"
#include "class_varibles.h"
#include "class_tile.h"
#include "tile_load.h"

extern SDL_Rect camera;
extern SDL_Event event;

// the players dimensions
const int player_width = 32;
const int player_height = 32;

using namespace std;

class player{

private:
//the players collision box
SDL_Rect box;

// the velocity of the player
int xvel, yvel;

public:
// initializes the varibles
player();

//takes key presses and adjsuts the players velocity
void handle_input();

// moves the player
void move(Tile *tiles[]);

// shows the dot on screen
void show();

//sets the camera over the dot
void set_camera();

};

#endif





basic_functions.cpp
#include "SDL.h"
#include "main.h"
#include "class_tile.h"
#include "graphics.h"
#include "tile_load.h"
#include "class_player.h"




// event checks if player xed out window
void x_quit(void)
{

if( event.type == SDL_QUIT )
{
//Quit the program
running = false;
}

}

void clean_up( Tile *tiles[] )
{
//Free the surfaces
SDL_FreeSurface( surface_player );
SDL_FreeSurface( surface_tilesheet );

//Free the tiles
for( int t = 0; t < TOTAL_TILES; t++ )
{
delete tiles[ t ];
}

//Quit SDL
SDL_Quit();
}


basic_functions.h
void clean_up( Tile *tiles[] )

Yes i know i didn't include the cleanup function but it is causing problems and is not needed at the moment.
Where is the declaration of "clips"? Is it properly declared extern in any header files, and defined in exactly one source file?

If you read my post, you might see how the compiler thinks that you have defined this variable in two translation units. If it is defined in a header that is included more than once this will be the case.

The key point is that the compiler doesn't see header files, or source files. These are destroyed by the preprocessing stage. It sees translation units. The linker sees compiled translation units, called object files. The toolchain isn't smart enough to work backwards to tell you where the errant definitions really came from.

This is what happens when a compilation process designed for computers with less than 16K of RAM circa 1970 is still used relatively unchanged today. It ain't your fault it is confusing - in fact it is a little braindead!

This topic is closed to new replies.

Advertisement