Sign in to follow this  
Thekill473

Another c++ error

Recommended Posts

Im sorry to post about another error, I am new to c++. My errors this time are
[code]Error 1 error C2143: syntax error : missing ';' before '*'
Error 2 error C4430: missing type specifier - int assumed.
Error 3 error C2065: 'TOTAL_TILES' : undeclared identifier
Error 4 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
[/code]

I do have literally 294 errors but I figure its only about 20-30 errors causing all of the other errors.
anyway here is the code. note that I moved [code]extern Tile *tiles[TOTAL_TILES];[/code] because it seemed more suited there.

main.cpp
[code]#include "SDL.h"
#include "basic_functions.h"
#include "graphic_functions.h"
#include "tile_load.h"
#include "graphics.h"
#include "class_player.h"
#include "class_tile.h"
#include "collision.h"

// main surfaces
// the screen surface
SDL_Surface* surface_screen;
// the surface for the player

///////////////varibles///////////////////////
//
// if this is off then the game quits //
bool running = true; //
//
// the width and height of the level //
// will need to be changed so that //
// the world is one continuios map //
const int level_width = 1000; //
const int level_height = 1000; //
//
// the screens dimensions //
const int screen_width = 960; //
const int screen_height = 960; //
const int screen_bbp = 32; //
//
// the frames per second regulator //
const int frames_per_Second = 20; //
//////////////varibles end////////////////////

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

// the tile varible
Tile *tiles[TOTAL_TILES];

// events
SDL_Event event;

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

SDL_Init(SDL_INIT_EVERYTHING);

surface_screen = SDL_SetVideoMode(screen_width, screen_height, 32, SDL_SWSURFACE);

load_tile_var();

//laods the dotclass
player mainplayer;



// load maps and map things
clip_tiles();

while(running == true)
{

// main loop

//if sdl finds an event
if(SDL_PollEvent(&event))
{

x_quit();

// checks if player has moved
// if player has moved then it moves the sprite
// at the draw sprite function
mainplayer.handle_input();

}

//loads the graphics
load_graphics();

// draws the background so that the sprites can erase properly
spr_background();

//loads the test map
// place holder [load_map("maps\\map.map");]

mainplayer.move(tiles);

//show tiles
for(int t = 0; t < TOTAL_TILES; t++){
tiles[t]->show();
}

// game start



// draws the players surface
mainplayer.show();

// flips the screen and draws all new graphics
SDL_Flip(surface_screen);

}


SDL_Quit();

return 0;

}[/code]

and main.h
[code]#pragma once
#include "SDL.h"
#include "tile_load.h"

//varibles
extern bool running;

extern const int level_width;
extern const int level_height;

extern const int screen_width;
extern const int screen_height;
extern const int screen_bbp;
extern Tile *tiles[TOTAL_TILES];

// camera

extern SDL_Rect camera;

//events
extern SDL_Event event;

// surfaces
extern SDL_Surface* surface_screen;[/code]

And you may think that I haven't even looked up my errors, but i spent 2 hours last night googling my errors.
if anymore information or files is required please ask. thanks for your time.

Share this post


Link to post
Share on other sites
Hi there,

You will have to excuse me if I am mistaken here (I have not coded C++ in a good few months, been stuck on java lately), but from what I'm seeing here I can say the following:

First off, howcome you're using the "extern" keyword on all your variables? Is this necessary? Second, what is "Tile"? This is an object you have created, right? Well, you probably need to declare that on the heap, using the "new" keyword. Third, your use of "TOTAL_TILES" - as the error states... what is this? The all-caps imply that this is a constant - but where is it declared? This needs to have a value or must at least be assigned a type (an integer, basically).

Also... on another note: I am assuming that the array of tiles are all your ingame tiles, yes? Well, using a 1-dimensional array for that means you'll have one long line of tiles , unless you have algorithms of some kind to rearrange these graphically. I would suggest a 2 or 3 dimensional array instead, depending on what you're trying to do there.

I hope this helps?

Share this post


Link to post
Share on other sites
I can't believe your compiler does not print the lines of the errors. You need to find a new compiler.

The meaning of the first error is that a type is not defined.
And there is a pointer to it.

The only lines with pointers to types are:

[code]
Tile *tiles[TOTAL_TILES];
SDL_Surface* surface_screen;
char* argv[]
extern Tile *tiles[TOTAL_TILES];
extern SDL_Surface* surface_screen;[/code]


Since char is recognized internally, this is not the error.
Therefore either SDL_Surface is not defined or Tile is not defined.
Since you just moved “tiles”, that would be a likely candidate, especially considering that TOTAL_TILES is also not defined. Include the proper headers.


L. Spiro

Share this post


Link to post
Share on other sites
[color=#660066]Error[/color][color=#000000] [/color][color=#006666]1[/color][color=#000000] error C2143[/color][color=#666600]:[/color][color=#000000] syntax error [/color][color=#666600]:[/color][color=#000000] missing [/color][color=#008800]';'[/color][color=#000000] before [/color][color=#008800]'*'[/color][color=#000000]
[/color][color=#660066]Error[/color][color=#000000] [/color][color=#006666]2[/color][color=#000000] error C4430[/color][color=#666600]:[/color][color=#000000] missing type specifier [/color][color=#666600]-[/color][color=#000000] [/color][color=#000088]int[/color][color=#000000] assumed[/color][color=#666600].[/color][color=#000000]
[/color][color=#660066]Error[/color][color=#000000] [/color][color=#006666]3[/color][color=#000000] error C2065[/color][color=#666600]:[/color][color=#000000] [/color][color=#008800]'TOTAL_TILES'[/color][color=#000000] [/color][color=#666600]:[/color][color=#000000] undeclared identifier
[/color][color=#660066]Error[/color][color=#000000] [/color][color=#006666]4[/color][color=#000000] error C4430[/color][color=#666600]:[/color][color=#000000] missing type specifier [/color][color=#666600]-[/color][color=#000000] [/color][color=#000088]int[/color][color=#000000] assumed[/color][color=#666600].[/color][color=#000000] [/color][color=#660066]Note[/color][color=#666600]:[/color][color=#000000] C[/color][color=#666600]++[/color][color=#000000] does [/color][color=#000088]not[/color][color=#000000] support [/color][color=#000088]default[/color][color=#666600]-[/color][color=#000088]int[/color][color=#000000] [/color]






In your actual error messages (not these modified ones) you will know which line it is on.

Error 1 is pretty obvious -- you are missing a semicolon somewhere. Look at the line before the line in the error. You might have missed a semicolon, or a parenthesis, or some other symbol. We'd need to know exactly which line it is, and the line before it, to help with that.

Error 2 is probably caused by error 1.

Error 3 (the TOTAL_TILES error) is likely from your main.h, but again check the actual line number. You are using it before it is declared. You wrote that you moved it because "it is more suited there", but if it causes compiler errors than obviously it is not. You would probably be fine with a simple pointer there since it is an extern declaration, but it depends entirely on what you are trying to do with it.

Error 4 is probably caused by error 3.




Share this post


Link to post
Share on other sites
[quote name='frob' timestamp='1318836538' post='4873359']Error 1 is pretty obvious -- you are missing a semicolon somewhere. Look at the line before the line in the error. You might have missed a semicolon, or a parenthesis, or some other symbol. We'd need to know exactly which line it is, and the line before it, to help with that.[/quote]
I would normally not say anything but the original poster may end up searching a long time for this.
In this case it actually means Tile is not a defined type. The error is regarding Tile *tiles[TOTAL_TILES]; or extern Tile *tiles[TOTAL_TILES];.
When he includes the proper file, all errors will go away.


L. Spiro

Share this post


Link to post
Share on other sites
Ok thanks for the help so far

J-dog
[quote]First off, howcome you're using the "extern" keyword on all your variables?[/quote]
Stupid of me :P sorry i didn't realize that i had the externs there. I got rid of the declaration in main.cpp
and declared them in main.h

[quote]Second, what is "Tile"? This is an object you have created, right? Well, you probably need to declare that on the heap, using the "new" keyword.[/quote]
How would I do this.

[code]Third, your use of "TOTAL_TILES" - as the error states... what is this? The all-caps imply that this is a constant - but where is it declared? This needs to have a value or must at least be assigned a type (an integer, basically). [/code]
It has been declared and the proper header has been used, I'll show you the code where it's been declared

tile_load.h
[code]#ifndef TILE_LOAD_H
#define TILE_LOAD_H

#include "class_tile.h"
#include "main.h"

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

// diffrent tile sprites
const int tile_green 0;
const int tile_black 1;

//functions

extern bool set_tiles(Tile *tiles[]);
extern bool touches_wall(SDL_Rect box, Tile *tiles[]);
extern void load_tile_var(void);

#endif[/code]

[quote]Also... on another note: I am assuming that the array of tiles are all your ingame tiles, yes? Well, using a 1-dimensional array for that means you'll have one long line of tiles , unless you have algorithms of some kind to rearrange these graphically. I would suggest a 2 or 3 dimensional array instead, depending on what you're trying to do there.[/quote]
Yes i do use a kind of algorithm for that.

[code]bool set_tiles(Tile *tiles[]){

//the tiles offsets
int x = 0, y = 0;

// open the map
std::ifstream map("maps//map.map");

//if the map couldnt be loaded
if(map == NULL){
return false;
}

//initialize the tiles
for(int t = 0; t < TOTAL_TILES; t++){

//what kind of tile will be made
int tiletype = -1;

//read tile from map
map >> tiletype;

//if there was a problem reading the map
if(map.fail() == true){

//stop laoding map
map.close();
return false;

}

//if the number is a valid tile
if((tiletype >= 0 ) && (tiletype < TILE_SPRITES)){
tiles[t] = new Tile(x, y, tiletype);
}

// if we dont recognize the tile type
else{

//stop loading map
map.close();
return false;
}

//move to th next tile spot
x += TILE_WIDTH;

//if we've gone to far
if( x >= level_width){

//move back
x = 0;

//move to the next row
y += TILE_HEIGHT;

}

// clsoe file
map.close();

//if the map loaded
return true;

}
}[/code]

YogurtEmperor
[quote]I can't believe your compiler does not print the lines of the errors. You need to find a new compiler.[/quote]
I use Microsoft visual 2010 express and yes it does display lines I just accidentally cut them off, My bad there. Here are they error codes
with the line numbers.
[code]
Error: Line: column:
Error 1 error C2143: syntax error : missing ';' before '*' 20 1
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 20 1
Error 3 error C2065: 'TOTAL_TILES' : undeclared identifier 20 1
Error 4 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 20 1
[/code]

Thanks again for all the help so far.

Share this post


Link to post
Share on other sites
Line 20 in what file?
There is nothing on line 20 in either file.

[quote]Error 2 error C4430: missing type specifier - int assumed.[/quote]
This error means you did not declare something.
Error #1 means an unknown identifier precedes a * (pointer).

The error is either Tile *tiles[TOTAL_TILES]; or extern Tile *tiles[TOTAL_TILES];.
Specifically, [b]Tile[/b] has not been defined because a header file is missing.


When you post compiler errors, [b]always[/b] post the line numbers and the file names.


L. Spiro

Share this post


Link to post
Share on other sites
Ok here is the error list

[code]
Error 1 error C2143: syntax error : missing ';' before '*' main.h 21 1
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int main.h 21 1
Error 3 error C2065: 'TOTAL_TILES' : undeclared identifier main.h 21 1
Error 4 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int main.h 21 1
[/code]

Thanks for being patient with me.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
What is the reason behind your [b]tile_load.h[/b] including [b]main.h[/b]? I don't see a reason for it to be included. Also, your [b]main.h[/b] and [b]main.cpp[/b] both include [b]tile_load.h[/b]. 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.

Share this post


Link to post
Share on other sites
The inclusion of [b]main.h[/b] could very well be the issue. I, having nothing better to do :P, threw together a minimal example project and, if I include [b]main.h[/b] in my [b]tile.h[/b], I get the same errors as you...
[code]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[/code]

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

tile.h[code]#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[/code]
tile.cpp[code]#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_;
}[/code]
main.h[code]#ifndef MAIN_H
#define MAIN_H

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

extern Tile *tiles[TOTAL_TILES];

#endif // MAIN_H[/code]
main.cpp[code]#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;
}[/code]
Compile it and test, then un-comment the [b]//#include "main.h"[/b] in [b]tile.h[/b] and re-compile it.

Share this post


Link to post
Share on other sites
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
[code]
#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
[/code]

tile_load.h
[code]
#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
[/code]

main.h
[code]
#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
[/code]

Now imagine our main function source file:
[code]
#include "main.h"

int main()
{
set_tiles(tiles);
}
[/code]
Let us look at what the tools do when compiling main.cpp. First, the file must be preprocessed to create a [i]translation unit[/i].

Here is our translation unit, annotated with comments to explain the preprocessor logic:
[code]
// 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);
}
[/code]
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.

Share this post


Link to post
Share on other sites
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.

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

class_player.cpp
[code]#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));

}[/code]

class_player.h
[code]#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



[/code]

basic_functions.cpp
[code]#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();
}
[/code]

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

Yes i know i didn't include the cleanup function but it is causing problems and is not needed at the moment.

Share this post


Link to post
Share on other sites
Where is the declaration of "clips"? Is it properly declared [i]extern[/i] 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!

Share this post


Link to post
Share on other sites
[quote name='Thekill473' timestamp='1319004099' post='4874156']
gahh. now i get this after my project compiles.
[code]Unhandled exception at 0x76ed15ee in game.exe: 0xC0000005: Access violation reading location 0x00000008.[/code]
[/quote]

You need to run it in the debugger to see where this happens and go from there.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this