Sprite' does not name a type

Started by
18 comments, last by rip-off 18 years, 5 months ago
Hey, I'm getting an error for whenever I try to create an instance of the class Sprite (within my other class). The three lines are: Sprite spr_helmaroc_left; Sprite spr_helmaroc_right; Sprite sprite_index; And the three errors are: helmaroc.h:20: error: `Sprite' does not name a type helmaroc.h:21: error: `Sprite' does not name a type helmaroc.h:22: error: `Sprite' does not name a type I'm not entirely sure what this means, so could someone tell me, and then help me fix this, please.
Advertisement
can we see sprite.h / sprite.cpp please.

its hard to see whats wrong, have you #included the header?
Could you paste the code in [sour ce lang="cpp"]
[/sour ce] tags please.

ace
The includes for the erroring file:
#include "sprite.h"

Sprite.h:
//sprite.h#ifndef SPRITE_H_INC#define SPRITE_H_INC#include <allegro.h>#include "vardec.h"extern int safe_sprite_ids;class Sprite {      public:      int offset_x;      int offset_y;      int max_frames;      int width;      int height;      int id;      bool operator == (Sprite &class1);      BITMAP ** frames;      Sprite(BITMAP * load_sprite, int load_width, int load_height, int frame_num, int load_offset_x = 0, int load_offset_y  = 0, int offset_x = 0, int offset_y = 0);      ~Sprite();      void draw(int x, int y, int frame, BITMAP * destination);      };void mirrorSprite(Sprite &mirror);int get_new_sprite_id();#endif //SPRITE_H_INC


Sprite.cpp:
//sprite.cpp#ifndef SPRITE_INC#define SPRITE_INC#include <allegro.h>#include <math.h>#include "sprite.h"#include "vardec.h"int safe_sprite_ids = 0;int get_safe_id(){    safe_sprite_ids++;    return safe_sprite_ids;  } Sprite::Sprite(BITMAP* load_sprite, int load_width, int load_height, int frame_num, int load_offset_x, int load_offset_y, int draw_offset_x, int draw_offset_y ) {   if (load_sprite != NULL){      id = get_safe_id();      frames = new BITMAP* [frame_num];      width = abs(load_width);      height = load_height;      max_frames = frame_num;      offset_x = draw_offset_x;      offset_y = draw_offset_y;      for (int a = 0; a < frame_num; a++){          frames[a] = create_bitmap(width, height);          blit(load_sprite, frames[a], load_offset_x + a*width, load_offset_y, 0, 0, width, height);      }      destroy_bitmap(load_sprite);   }   else allegro_message("Error loading a certain sprite");}Sprite::~Sprite(){for (int a = 0; a< max_frames; a++)destroy_bitmap(frames[a]);}void mirrorSprite(Sprite &mirror){     for (int a = 0; a< mirror.max_frames; a++){     BITMAP * temp = create_bitmap(mirror.width, mirror.height);     clear_to_color(temp, makecol(255, 0, 255) );     draw_sprite_h_flip(temp, mirror.frames[a], 0, 0);     blit(temp, mirror.frames[a], 0, 0, 0, 0, mirror.width, mirror.height);     destroy_bitmap(temp);     }}bool Sprite::operator==(Sprite &class1){     return (id == class1.id);}void Sprite::draw(int x, int y, int frame, BITMAP * destination = sbuffer){     masked_blit(frames[frame], destination, 0, 0, x - offset_x, y - offset_y, width, height);}#endif //Sprite_INC
umm you dont #include sprite.cpp do you?

because if you don't why do you have include guards there...
Because when I originally learned #include guards, I wasn't told not to put them on .cpps
Besides it's only there once, isn't it? So it doesn't matter.
That's not the problem.
Why have you got inclusion guards around the .CPP code?

ace
Once again, I don't know really, I just have? But that's not the problem.
I'll take them off if you want.
the only thing i can think of is some place, maybe in the #includes of sprite.h, thee is a missing '}' at the end of a function/class or ';' at the end of a class.

look at vardec.h for things like that, and also in the code that creates the sprites (helmaroc.h)

and i guess ace_lovegrove didnt see my or your replys before he replied himself...
A quick google search suggests that that error can occur if you have code like:
class Sprite{};int main(){	int Sprite;	Sprite spr_helmaroc_left;}

So check that you're not using Sprite as a variable name anywhere in your code at the same scope as your error-causing declarations.
A few other comments on your code:
  1. #include <math.h> is deprecated in C++. You should be using #include <cmath> instead. This also puts all the symbols into namespace std (i.e. std::abs).

  2. There should be no need to wrap Sprite.cpp (or indeed any cpp file unless it contains template definitions and is included by a header) in header guards. There's no harm in doing it (unless you mistakenly reuse the header's guard) but it's not really neccessary.

  3. It looks like get_safe_id is only used by Sprite. If this is true then it should not be exposed to the rest of your program in the header file. Instead consider putting it and safe_sprite_ids into the unnamed namespace in Sprite.cpp, i.e.:
    namespace{	int safe_sprite_ids = 0;	int get_safe_id()	{		// ...	}}
    and remove the corresponding declarations from Sprite.h.

  4. Your data members should certainly not all be public. As it stands anyone can change max_frames and cause the Sprite class to access invalid memory. Encapsulation is good.

  5. You leak frames. You allocate it with new[] but you never delete[] it.

  6. Sprites are not safe to copy. Obey the law of three (if you need one of the copy constructor, destructor and copy assignment operator you generally need all three). I would advise a std::vector< boost::shared_ptr< BITMAP > > or a boost::shared_array< BITMAP >. In both cases you would need to provide the correct deleter to the boost smart pointer.

  7. In the event that your Sprite constructor fails you leave the instance with uninitialised data, but do not signal the failure to the calling code. You should either initialise all data to default values, set a fail flag or (my preference) throw an exception.

Enigma

This topic is closed to new replies.

Advertisement