Sign in to follow this  
Thekill473

c++ error please help

Recommended Posts

hello everybody right now i'm having with clipping my tile sheet. Its probably a really dumb error on my part but anyways here is the code.

graphic_functions.cpp
[code]#include"SDL.h"
#include <string>
#include "SDL_image.h"
#include "tile_load.h"
#include "class_tile.h"

using namespace::std;

// used for clipping
SDL_Rect clips[TILE_SPRITES];

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL)
{
//Holds offsets
SDL_Rect offset;

//Get offsets
offset.x = x;
offset.y = y;

//Blit
SDL_BlitSurface( source, clip, destination, &offset );
}

void draw_sprite(int srcX, int srcY, int dstX, int dstY, int width, int height, SDL_Surface* source, SDL_Surface* destination)
{

SDL_Rect src;
src.x = srcX;
src.y = srcY;
src.w = width;
src.h = height;

SDL_Rect dst;
dst.x = dstX;
dst.y = dstY;
src.w = width;
src.h = height;

SDL_BlitSurface(source, &src, destination, &dst);

}

SDL_Surface *load_image( std::string filename )
{
//The image that's loaded
SDL_Surface* loadedImage = NULL;

//The optimized surface that will be used
SDL_Surface* optimizedImage = NULL;

//Load the image
loadedImage = IMG_Load( filename.c_str() );

//If the image loaded
if( loadedImage != NULL )
{
//Create an optimized surface
optimizedImage = SDL_DisplayFormat( loadedImage );

//Free the old surface
SDL_FreeSurface( loadedImage );

//If the surface was optimized
if( optimizedImage != NULL )
{
//Color key surface
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
}
}

//Return the optimized surface
return optimizedImage;
}


void clip_tiles()
{
//Clip the sprite sheet
clips[tile_green].x = 0;
clips[tile_green].y = 0;
clips[tile_green].w = TILE_WIDTH;
clips[tile_green].h = TILE_HEIGHT;

clips[tile_black].x = 0;
clips[tile_black].y = 32;
clips[tile_black].w = TILE_WIDTH;
clips[tile_black].h = TILE_HEIGHT;

}



[/code]
please help.

Share this post


Link to post
Share on other sites
sorry about the double post I didn't know it posted cause I got this error with my web browser when i clicked post.
hello everybody right now I'm having with clipping my tile sheet. Its probably a really dumb error on my part but anyways here is the code.

gamedev.net mySQL query error: SELECT cs_value, cs_updated FROM cache_store WHERE cs_key='mail_processing

-_- my computer just keeps spitting errors at me

Share this post


Link to post
Share on other sites
is this the line with the error? [code]SDL_Rect clips[TILE_SPRITES];[/code]
Array size must be fixed at compile time so you need to use a constant expression when giving the length. TILE_SPRITES should be a constant.

Share this post


Link to post
Share on other sites
Oh sorry i forgot to add the error code :P.

here is the error code.
[code] 4 IntelliSense: expression must have a constant value c:\users\tyler_2\desktop\sattriah\sattriah\sdl tutorial\graphic_functions.cpp 10 16 sdl tutorial
[/code]

And here is were i declare "TILE_SPRITES".

tile_load.cpp
[code]#include "SDL.h"
#include "graphic_functions.h"
#include "main.h"
#include "class_tile.h"
#include <iostream>
#include <fstream>
#include <string>
#include "collision.h"

//Tile constant varibles

//Tile constants
const int TILE_WIDTH = 80;
const int TILE_HEIGHT = 80;
const int TOTAL_TILES = 192;
const int TILE_SPRITES = 2;

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

//the tile class

void load_tile_var(void){

Tile *tiles[TOTAL_TILES];

}

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;

}
}

bool touches_wall( SDL_Rect box, Tile *tiles[] )
{
//Go through the tiles
for( int t = 0; t < TOTAL_TILES; t++ )
{
//If the tile is a wall type tile
if( ( tiles[ t ]->get_type() >= tile_green ))
{
//If the collision box touches the wall tile
if( check_collision( box, tiles[ t ]->get_box() ) == true )
{
return true;
}
}
}

//If no wall tiles were touched
return false;
}

[/code]

And the header file

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

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

//varibles
extern const int TILE_WIDTH;
extern const int TILE_HEIGHT;
extern const int TOTAL_TILES;
extern const int TILE_SPRITES;

extern const int tile_green;
extern const int tile_black;
extern tile *tiles[TOTAL_TILES];

//functions

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

#endif[/code]

Share this post


Link to post
Share on other sites
The problem is that you use extern declaration of TOTAL_TILES in the header. Put the definition of TOTAL_TILES in the header instead and it should work.

Share this post


Link to post
Share on other sites
[quote name='Thekill473' timestamp='1318390663' post='4871713']
Oh sorry i forgot to add the error code :P.

here is the error code.
[code] 4 IntelliSense: expression must have a constant value c:\users\tyler_2\desktop\sattriah\sattriah\sdl tutorial\graphic_functions.cpp 10 16 sdl tutorial
[/code][/quote]

If I'm reading that correctly, and I'm counting correctly, that tells you that line 10, character 16 is where your error is at. Which makes it the [b]TILE_SPRITES[/b] constant, as Wooh previously asked about. That would tell me that your includes, or extern, aren't being used properly. I haven't analysed the code yet, so I can't comment on that...yet.

Share this post


Link to post
Share on other sites
Your constant is of type "const int"; the compiler might not make the jump that it is perfectly constant, and will likely throw an error upon trying to use it to declare an array.

For instance, illegal pointer aliasing might cause the value to change, causing an error. Most compilers avoid this entirely and throw an error in C89; in almost all language derivatives since then, global storage _must_ be a constant value, and not a variable labeled const.

Try using #define.

Share this post


Link to post
Share on other sites
[quote name='Ectara' timestamp='1318433611' post='4871875']
Your constant is of type "const int"; the compiler might not make the jump that it is perfectly constant, and will likely throw an error upon trying to use it to declare an array.
[/quote]
No. Any compiler out there that implements [b]const[/b] implements it correctly.
[quote]
For instance, illegal pointer aliasing might cause the value to change, causing an error. Most compilers avoid this entirely and throw an error in C89; in almost all language derivatives since then, global storage _must_ be a constant value, and not a variable labeled const.
[/quote]
I'm afraid this does not coincide with reality. The C89 language does not have const. C++ provides const, and const values may be in namespace scope with static storage duration without the compiler complaining,and they will work as specified by the language. I will not comment on other languages derived from C89.
[quote]
Try using #define.
[/quote]
Do not take this advice. It is bad advice.

The answer is that an extern declaration is not a const value. If you want a const, define a const. It's OK to define const ints at namespace level in header files.

Share this post


Link to post
Share on other sites
[quote name='Bregma' timestamp='1318518445' post='4872212']
[quote]
Try using #define.
[/quote]
Do not take this advice. It is bad advice.
[/quote]

#define is bad advice? Since when?

Share this post


Link to post
Share on other sites
[quote name='Ectara' timestamp='1318548715' post='4872349']
#define is bad advice? Since when?
[/quote]
Since forever. Dumb text replacement that doesn't understand the language should generally be avoided when superior methods exist.

In C, you don't get much choice. In C++, half of the features were attempts to replace ad-hoc preprocessor workarounds with something that actually expressed intent, something the compiler actually understands and can issue meaningful warnings or errors on.

In my opinion one should think very hard before using the preprocessor for anything other than include guards and conditional compilation.

Share this post


Link to post
Share on other sites
[quote name='rip-off' timestamp='1318581093' post='4872449']
[quote name='Ectara' timestamp='1318548715' post='4872349']
#define is bad advice? Since when?
[/quote]
Since forever. Dumb text replacement that doesn't understand the language should generally be avoided when superior methods exist.

In C, you don't get much choice. In C++, half of the features were attempts to replace ad-hoc preprocessor workarounds with something that actually expressed intent, something the compiler actually understands and can issue meaningful warnings or errors on.

In my opinion one should think very hard before using the preprocessor for anything other than include guards and conditional compilation.
[/quote]

Well, I apologize. I appreciate the explanation rather than just blindly calling me out.

I only ever use C89, but I figured I'd at least try to contribute to the solution.

Share this post


Link to post
Share on other sites
[quote name='Bregma' timestamp='1318518445' post='4872212']
I'm afraid this does not coincide with reality. The C89 language does not have const. C++ provides const, and const values may be in namespace scope with static storage duration without the compiler complaining,and they will work as specified by the language. I will not comment on other languages derived from C89.
[/quote]

Also, since when does C89 not have const?
http://en.wikibooks.org/wiki/C_Programming/Reference_Tables#List_of_Keywords
http://www.gnu.org/s/gnu-c-manual/gnu-c-manual.html#Keywords

Share this post


Link to post
Share on other sites
[quote name='Ectara' timestamp='1318594110' post='4872490']
Also, since when does C89 not have const?
http://en.wikibooks.org/wiki/C_Programming/Reference_Tables#List_of_Keywords
http://www.gnu.org/s/gnu-c-manual/gnu-c-manual.html#Keywords
[/quote]
I stand corrected. C89 did have a const keyword. None of the compilers I used into the 1990s supported that keyword (since most code was K&R C, it didn't much matter), but it is certainly specified in the ISO standard.

Share this post


Link to post
Share on other sites
[quote name='Bregma' timestamp='1318604759' post='4872525']
[quote name='Ectara' timestamp='1318594110' post='4872490']
Also, since when does C89 not have const?
[url="http://en.wikibooks.org/wiki/C_Programming/Reference_Tables#List_of_Keywords"]http://en.wikibooks....ist_of_Keywords[/url]
[url="http://www.gnu.org/s/gnu-c-manual/gnu-c-manual.html#Keywords"]http://www.gnu.org/s...l.html#Keywords[/url]
[/quote]
I stand corrected. C89 did have a const keyword. None of the compilers I used into the 1990s supported that keyword (since most code was K&R C, it didn't much matter), but it is certainly specified in the ISO standard.
[/quote]

Fair enough, good sir.

But, yeah, extern would definitely break const guarantees. I hadn't noticed the extern declaration on the first read.

Share this post


Link to post
Share on other sites
[quote]The answer is that an extern declaration is not a const value. If you want a const, define a const. It's OK to define const ints at namespace level in header files. [/quote]

Ok I'm fairly new to programming so how do I define the variable in the header file
would it be.
[code] #define SDL_Rect clips[TILE_SPRITES];[/code]

Or define without the hash?

Share this post


Link to post
Share on other sites
Define is an overloaded term. You are talking about the preprocessor directive #define. We are talking about the [i]definition[/i] of an object. The definition of a function is it's code and the definition of a global variable is initial value, default value or constructor call. Classes also have definitions, but that is a little different.

#define is a dumb text replacement, so you should almost always avoid using it. The code you posted will cause a text replacement of all instances of "SDL_Rect" to be replaced by "clips[TILE_SPRITES];". Expect some seriously confusing compiler errors.

The quick solution is to move the constant [i]definitions[/i] to the header file:
[code]
#ifndef TILE_LOAD_H
#define TILE_LOAD_H

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

//varibles
const int TILE_WIDTH = 80;
const int TILE_HEIGHT = 80;
const int TOTAL_TILES = 192;
const int TILE_SPRITES = 2;

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

extern tile *tiles[TOTAL_TILES];

//functions

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

#endif
[/code]
Note that the "tiles" variable is still [u]extern[/u], because it is not a constant. Const variables are have implicit internal linkage, so this will not cause multiple definition linker errors. Also note that function declarations are implicitly [u]extern[/u]. I've removed the redundant keyword here.

In tile_load.cpp, simply omit the const variable definitions.

Share this post


Link to post
Share on other sites
Can't believe nobody else caught this.

[quote][code]
using namespace::std;[/code][/quote]

That should not have the "::" in there. Those are for separating parts of a namespace-path. Here, "namespace" is a keyword telling the compiler that "std" is supposed to be a namespace-name.

"using namespace::std" would be what you would write in order to "use" an identifier "std" from the "namespace" namespace. However, for hopefully obvious reasons, there cannot be a namespace named "namespace".

Share this post


Link to post
Share on other sites
[quote]
Can't believe nobody else caught this.
[/quote]
It is easy to skip over lines you assume are correct.

[quote]
That should not have the "::" in there. Those are for separating parts of a namespace-path. Here, "namespace" is a keyword telling the compiler that "std" is supposed to be a namespace-name.
[/quote]

This appears to be parsed as [u]using[/u] [u]namespace[/u] [u]::std[/u], in other words, needlessly qualifying the standard namespace with the global namespace. I have no problem compiling it with GCC.

But yes, an unusual practise, which should probably be discouraged.

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