Using own classes in other classes

Started by
14 comments, last by ZQJ 18 years, 8 months ago
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;
};

Advertisement
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.
----------------------------------------------------"Plant a tree. Remove a Bush" -A bumper sticker I saw.

put #include "headerfile A.h" at the top of 'headerfile B.h'

illone
illone
are you including headerfile A.h in headerfile B.h?


edit*

I guess we posted at the same time..
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.
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...
ship.h should #include sprite.h
----------------------------------------------------"Plant a tree. Remove a Bush" -A bumper sticker I saw.
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
An alternative would be to take up C# [smile].
Rob Loach [Website] [Projects] [Contact]
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...
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________

This topic is closed to new replies.

Advertisement