so in gameWindow.h I have : gameBalls* balls;
in gameWindow.cpp I have to include a gameBalls object so I pass it in the constructor (as a pointer to gameBalls).
as in: gameWindow(gameBalls* balls) : balls(balls) {}
in gameBalls constructor I initialze a pointer to surface
gameBalls::gameBalls() {
pBalls = new SDL_Surface(); // SDL_Surface* pBalls in gameBalls.h
then in main I create a gameWindow object (pass it a gameBalls object pointer)
gameWindow window = new gameWindow(&balls);
and try to access this balls pointer to pBalls in gameWindow.cpp
as so : balls->pBalls .... this creates an error ?
any ideas why ?
hope somebody can help thanks so much :)
You can't access pBalls from outside the class if it's a private member.
oOoOo wrote:
pBalls = new SDL_Surface();
SDL is a C library so it doesn't have constructors and destructors. Instead you need to use the functions provided by the SDL library to construct and destruct the objects.
I removed the pBalls = new SDL_Surface(); statement from the gameBalls constructor but it still
crashes at: balls->pBalls = IMG_Load("ball1.png"); //in gameWindow.cpp
You have just created a pointer but you have not set it to point anywhere.
You can create the gameBalls object with new as you have done with your other objects.
1 2
gameBalls* balls1 = new gameBalls;
gameWindow* window = new gameWindow(balls1);
Pointers are error prone and often make the code more complicated than it has to be so you might want to consider not using them when you don't have to.
I would , but in order to add the gameBalls object to gameWindow it needs to be a pointer? I think?
I tried adding it as a normal object but I got errors about it .
I changed my code with added new , but it still crashes (for other reasons). I doubt you will want to take a look?