need to pass a C++ object to another class

Started by
24 comments, last by redwingdw 17 years, 8 months ago
Quote:
class Trajectory; <-------this is a trajectory forwrd declaration in the Ball.h file but it doesnt fix the Trajectory move line
class Ball public shape {
Trajectory movements; //creates a default trajectory object to be gotten at a later time

}


where I do Trajectory movements in the class header I get an error and I have done a forward declaration but it still doesnt work , thats what I was getting at in the end. How could I fix this? If its a forward declaration why is it not working ? and which other class is it needed in if not the above?

Yes I have done as enigma has suggested and solved the circle problem but I know have a problem with Trajectory moved; line even though I forward declared it

thanks
Advertisement
If class A contains a member of type class B then the full definition of class B is required, a forward definition is not sufficient. So Ball will have to #include "Trajectory.h", since it contains a member variable of type Trajectory.

Σnigma
I tried that earlier then got a "Class type re-definition error" but I put in a
#ifndef TRAJECTORY_H
#define TRAJECTORY_H

class header

#endif

which seemed to fix it :-), I also found a good article that Ive just started reading which I will submit. That stuff you submitted above has really helped enigma !

cheers all
David

http://www.gamedev.net/reference/articles/article1798.asp
Quote:Original post by redwingdw
http://www.gamedev.net/reference/articles/article1798.asp

That's the article I was talking about before. Great that you found it - I've finally gotten around to bookmarking it myself now!

Σnigma
No problem :-)


I have one more problem , I have read the article and I have a few global variables which used to be in main.cpp anyway I put them in a file Globals.h:

#ifndef GLOBALS_H
#define GLOBALS_H
#include <SDL/SDL.h>
#include <stdio.h>
#include<string>
#include <SDL/SDL_image.h>
#include<iostream.h>
//set global vars for screen width height and bts per pixel they are constants


extern const int SCREEN_WIDTH;
extern const int SCREEN_HEIGHT;
extern const int SCREEN_BPP;
extern const int BALL_SPEED;
extern const int BAT_SPEED;


//The frame rate
extern const int FRAMES_PER_SECOND;


//set dot attribs
extern const int ball_h;
extern const int ball_w;

//set global surfaces
//SDL_Surface *dot = NULL;
extern SDL_Surface *screen;
extern SDL_Surface *bat_player;
extern SDL_Surface *bat_cpu;
extern SDL_Surface *ball;
extern SDL_Rect* clip;
//The event structure
extern SDL_Event event;
#endif

I also have a file globals.cpp with the variable immplementation:

#include "Globals.h"
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
const int BALL_SPEED = 2;
const int BAT_SPEED = 1;


//The frame rate
const int FRAMES_PER_SECOND = 20;


//set dot attribs
const int ball_h = 40;
const int ball_w = 40;

//set global surfaces
//SDL_Surface *dot = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *bat_player = NULL;
SDL_Surface *bat_cpu = NULL;
SDL_Surface *ball = NULL;
SDL_Rect *clip = NULL;
SDL_Event event;

the problem is in main when I call

sdl_init it fails to init the screen , this worked no problem before having includes and Im thinking its to do with the globals file : heres my main includes

#include <SDL/SDL.h>
#include <string>
#include <stdio.h>
#include <vector>
#include <SDL/SDL_image.h>
#include<iostream.h>
#include "Globals.h"
#include "Trajectory.h"
#include "PaddleP.h"
#include "PaddleC.h"
#include "Ball.h"
#include "Draw.h"
Yahoooo


Finally :-))))

sorry about that last comment , I had started another project and forgot to include the image files DOW!!!!

Well that is the first time Ive used includes and extern variables, Ive fixed this problem and Ive learned a heap so 100% no more questions from me on this thread :-))

This topic is closed to new replies.

Advertisement