Sign in to follow this  
walle

Using own classes in other classes

Recommended Posts

walle    478
Why can't I do like this?
//in headerfile A.h
class A
{
   public:
      A();
      ~A();
     
      //etc.
   private:
      //data
};

And then
//in headerfile B.h
class B
{
   public:
      B();
      ~B();
     
      //etc.
   private:
      A myclass;
};

Share this post


Link to post
Share on other sites
tHiSiSbOb    463
Uhh...You can. What makes you think you can't? Are you getting an error? If so, make sure you have all the include files you need included.

Share this post


Link to post
Share on other sites
Uncle Lemon    127
I think you need to put this line:

#include "A.h"

in the file B.h. That tells the linker to use the file (and classes in it). I've tried this method in C++ and it seems to work.

Share this post


Link to post
Share on other sites
walle    478
Yes...I know that I can...ok...here is how I do it:


//shared.h
#include "sprite.h"
//etc...





In sprite.h there is a decleration of the class sprite.

Then in ship.h:

#include "shared.h"
class ship
{
public:
//etc.
private:
sprite m_ship;
};





My compiler (Mingw) says that sprite doesn't name a type...and the other classes I have in other classes too.

And can there be problems if sprite.h use #include "shared.h" becuse it does that now...

Share this post


Link to post
Share on other sites
walle    478
Tried that...well I'll post all code...can't continue untill this is solved =(


#ifndef SPRITE_H
#define SPRITE_H

#include "shared.h"

// A single frame in the animation
struct frame
{
int delay; // The number of milliseconds to wait before next frame
std::string path; // The path to the frame
SDL_Surface *img; // The image of the frame
};

// A whole animation
struct animation
{
std::string name; // The name of the animation, the name of the folder the frames are in
int framenr; // Keeping track of which frame we are currently viewing
int frametime;
std::vector<frame> frames; // All frames

}; // A basic animated sprite
class sprite
{
public:
sprite();
~sprite();

// Self explained just look at the names
int getx();
int gety();
int getw();
int geth();
SDL_Rect getrect();
std::string getanimation();

void setx(int x);
void sety(int y);
void setw(int w);
void seth(int h);
void setrect(SDL_Rect rect);
void setanimation(std::string name);

void addx(int add);
void addy(int add);

// This function loads a sprite from a directory, first it checks how many different animations
// the sprite has, then loads the frames of every animation. The width and height of the sprite
// is decided by the first frame of the first animation
bool load(std::string dir, SDL_Surface *dest);

// This function draws the sprite, it animates automatically, if it isn't paused
void draw();

// Animation controls
void pause();
void resume();
void reset();

void setcolorkey(char r, char g, char b);

private:
SDL_Rect m_rect; // The rect of the sprite x,y,width and height values
int m_animation; // Keeps track of which animation we are on
int m_lastframe; // Keeps track of how many milliseconds it has been since the last frame
std::vector<animation> m_animations; // Every animation
SDL_Surface *m_dest; // The surface to draw to
bool m_paused; // Is the animation paused?
};

#endif





#ifndef SHARED_H
#define SHARED_H

#include <iostream>
#include <stdarg.h>
#include <fstream>
#include <string>
#include <vector>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_mixer.h>
#include <SDL/SDL_ttf.h>
#include <ctime>

#include "font.h"
#include "framerate.h"
#include "img_manager.h"
#include "log.h"
#include "settings_manager.h"
#include "sound_manager.h"
#include "sprite.h"

#include "game.h"
#include "ship.h"

#endif




#ifndef SHIP_H
#define SHIP_H

#include "shared.h"

class ship
{
public:
ship();
~ship();

void shoot();
void die();
bool collide();
private:
sprite m_ship;
int m_lives;
};

#endif


Share this post


Link to post
Share on other sites
Thunder_Hawk    314
I don't think this will fix your problem, but you should put inclusion guards on shared.h as well. I would suggest only including headers when necessary and dropping the concept of a shared header file alltogether as it tends to lead to these kinds of problems, but to each his own...

Share this post


Link to post
Share on other sites
walle    478
Quote:
Original post by Rob Loach
An alternative would be to take up C# [smile].


How come?

Well I have no idea on how it came to work...put inclusion guards on shared.h and then everything worked as suposed =) strange...thanks everyone and do go take a look at my webpage and vote on the talkhost topsites banner at the bottom [smile]

Share this post


Link to post
Share on other sites
Thunder_Hawk    314
Quote:
Original post by walle
Well I have no idea on how it came to work...put inclusion guards on shared.h and then everything worked as suposed =) strange...thanks everyone and do go take a look at my webpage and vote on the talkhost topsites banner at the bottom [smile]


*thinks for a moment*

The first time that sprite.h is included (from shared.h) it would then include shared.h, which would in turn skip including sprite.h again (because of sprite.h's inclusion guards) and continue on to include ship.h before the contents of sprite.h (the sprite class) were actually detailed, making the sprite class undefined at the time when the ship class was defined. Whew.....

This same problem would thus be exhibited in every case where you used a class that you defined yourself due to the cyclic nature of your inclusions. I really hope this encourages you to avoid this sort of thing in the future.

Share this post


Link to post
Share on other sites
walle    478
Quote:
Original post by Thunder_Hawk
I really hope this encourages you to avoid this sort of thing in the future.


Yes sir! I usually do that...don't know why I didn't do that in this case...

Share this post


Link to post
Share on other sites
ZQJ    496
Quote:
Original post by Boku San
You could also forward-declare class A in the B.h file.[/code]


That only works for pointers. If you want an instance of the class itself the class must have been defined before that point in the source.

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