SFML community forums

Help => General => Topic started by: Mokka on April 14, 2015, 05:37:55 pm

Title: C++ Problem
Post by: Mokka on April 14, 2015, 05:37:55 pm
After I fixed my linker problems, I stumbled upon another error.

I wrote a code for a small game and I created like a request if-loop to read out the keyboard inputs (Up, Down, Left, Right), and now, to clean up my code I want to put the loop in a seperate function, or even a seperate header file/class.

But I am not able to succeed :(

First of all: here is my code:

(click to show/hide)

The section //Move-Loop is the part I want to put in a seperate function.

So basically I just have to declare a function with a sf::Sprite as parameter which then represents the sprite that is supposed to get moved...

(click to show/hide)

But that does not work...
I am kinda new to C++ (as you can see :D) but was pretty stable with coding until this point.

So could anyone please help me.
Best with a working solution with outsourced classes (in a header file).


Thank you for your help in advance...

(I don't know if this is the right place to post this stuff. If not please tell me where to put it and I will copy it there and delete this post)
Title: Re: C++ Problem
Post by: eXpl0it3r on April 14, 2015, 05:50:30 pm
To properly work with SFML you need a good understanding of C++. Thus you should really get a good C++ book (http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and work through it. C++ is not a language you can learn with the trial-and-error approach, because you end up wasting a lot of time, learn a lot of bad things and miss out on various important topics.

As for you problem: You're passing the sprite by value, thus all the changes happen to a local instance and not the original sprite. Use references instead.
Title: Re: C++ Problem
Post by: Mokka on April 14, 2015, 06:00:25 pm
Thanks :D
Title: Re: C++ Problem
Post by: Red-XIII on April 14, 2015, 07:08:06 pm
To be more specific, you should look into pointers. That should help you figure out your problem, and you can continue to experience software engineering with SFML.
Title: Re: C++ Problem
Post by: Rosme on April 14, 2015, 07:19:26 pm
@Red-XIII: No. Just no. There is no need for pointers. References is the way to go. That was specific enough. Don't suggest pointers to a beginner. Don't talk about raw pointers. Talk about smart pointers instead. But again, no pointers here.
Title: Re: C++ Problem
Post by: Red-XIII on April 14, 2015, 07:26:40 pm
No, references will not help with his problem, he needs to use pointers, otherwise hes just copying the value, and not accessing the direct value.
Title: Re: C++ Problem
Post by: Nexus on April 14, 2015, 07:30:31 pm
No, references will not help with his problem, he needs to use pointers, otherwise hes just copying the value, and not accessing the direct value.
It seems like you should read up a bit on C++ too, as this is totally wrong ;)

As an indirection mechanism, both pointers or references can be used, none of them leads to a copy. Which one you use depends on various semantical criteria (can it be nullptr, do I need to change it after initialization, do I need pointer arithmetics...).
Title: Re: C++ Problem
Post by: Red-XIII on April 14, 2015, 07:35:15 pm
Oh I see, interesting, I thought pointers were the only way. My apologies.  :)