help with pointers?

closed account (N8RzwA7f)
hello,

I have a gameBalls class and a gameWindow class.

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 :)
oOoOo wrote:
balls->pBalls .... this creates an error

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.

To create a SDL_Surface you would have to use a function such as SDL_CreateRGBSurface or SDL_LoadBMP.
https://wiki.libsdl.org/SDL_CreateRGBSurface
https://wiki.libsdl.org/SDL_LoadBMP

To destroy a SDL_Surface you should use SDL_FreeSurface.
https://wiki.libsdl.org/SDL_FreeSurface
Last edited on
closed account (N8RzwA7f)
pBalls is public does that make a difference?
It just rules out one of many possible explanations. What does the error message say?
closed account (N8RzwA7f)
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

hope you can help :)
Last edited on
Does balls point to a valid gameBalls object?
closed account (N8RzwA7f)
I think so . I have in main:

gameBalls* balls1;
gameWindow* window = new gameWindow(balls1);


in gameWindow.h

private: gameBalls* balls1;

ing gameWindow.cpp:

gameWindow(gameBalls* balls1) balls1:balls1 {}
gameWindow::somefunction() { balls1->pBall= IMG_Load("ball1.png");

in gameBalls h:
public: SDL_Surface* pBall = NULL;



thanks! :)

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.
1
2
gameBalls balls1;
gameWindow window(balls1);
Last edited on
closed account (N8RzwA7f)
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?

thanks.
oOoOo wrote:
in order to add the gameBalls object to gameWindow it needs to be a pointer? I think?

You can always get a pointer to an object using the address-of operator &.

1
2
gameBalls ballsObject;
gameBalls* ballsPointer = &ballsObject;


In some situations you can use references as a substitute for pointers.

oOoOo wrote:
I changed my code with added new , but it still crashes (for other reasons). I doubt you will want to take a look?

I'm going to bed now. If you post the code someone else might be able to help, or I can have a look in the morning.
Last edited on
Topic archived. No new replies allowed.