Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: C++ Problem  (Read 2716 times)

0 Members and 2 Guests are viewing this topic.

Mokka

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
C++ Problem
« 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)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: C++ Problem
« Reply #1 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 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.
« Last Edit: April 14, 2015, 06:16:06 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Mokka

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: C++ Problem
« Reply #2 on: April 14, 2015, 06:00:25 pm »
Thanks :D

Red-XIII

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Advanced Protocol
    • Email
Re: C++ Problem
« Reply #3 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.
« Last Edit: April 14, 2015, 07:09:59 pm by Red-XIII »

Rosme

  • Full Member
  • ***
  • Posts: 169
  • Proud member of the shoe club
    • View Profile
    • Code-Concept
Re: C++ Problem
« Reply #4 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.
GitHub
Code Concept
Twitter
Rosme on IRC/Discord

Red-XIII

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Advanced Protocol
    • Email
Re: C++ Problem
« Reply #5 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.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: C++ Problem
« Reply #6 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...).
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Red-XIII

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Advanced Protocol
    • Email
Re: C++ Problem
« Reply #7 on: April 14, 2015, 07:35:15 pm »
Oh I see, interesting, I thought pointers were the only way. My apologies.  :)

 

anything