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

Author Topic: So close ...I just need a little push please :3  (Read 2672 times)

0 Members and 2 Guests are viewing this topic.

svladd Cjelik

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
So close ...I just need a little push please :3
« on: November 29, 2012, 02:56:26 am »
First off, thank you all so much for your help and advice so far.
 
Yes, I am learning C++ as I go -and I find the best way is just to jump right in the deep end.
 
I have been programming in C++ for about 6 months now and get the procedural stuff.  I also have a BASIC understanding of pointers, classes and linked lists and so on -I have played around with the old Clock::setTime() from the two text books I have, and they have been holding my hand every step of the way so far.
 
The thing is, I need just a little help please.
 
My code is here https://gist.github.com/4166189

...I am using SFML 2.0RC, and I have stripped the code in that post to the bare bones of exactly what is troubling me ...that is I have removed the grand bulk of the funtions and so on...
 
I have asked here before, but this time I have refined my question somewhat.
 
Lines 10, 12, and 14 - 17 of LoadMaps.cpp

Is the function definition correct?  I think I should be using the -> operator not the . operator though I can't seem to get the syntax right for the prototype.

Line 7 of LoadMaps.h

Is this the correct information if I want to pass the addresses of my data by reference?  Or are the ampisands not required because the pointers are addresses?

Line 28 of main.cpp

How should I call the function.
 
Basically yes, I am asking for the correct syntax, and for you to write it for me if you will.  As for understanding it, I will get there when I see it working =]
 
Thank you so very much for your patience.

Ruckamongus

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Re: So close ...I just need a little push please :3
« Reply #1 on: November 29, 2012, 04:20:52 am »
Lines 14 - 17:
Is correct because you aren't using pointers, you're using references. (They are C++'s way of passing things around. Essentially they are hidden pointers) Since you're using references the dot operator is correct as the "->" operator is only valid with pointers.

Line 7 of LoadMap.h:
Again, you will not be passing pointers around; the function will accept a reference because of the ampersand. If you wanted it to accept pointers (addresses) then use:
int loadWallsGFX(sf::RenderWindow*, sf::Texture*);

Line 28 of main.cpp:
Since you declared the variables as pointers you are calling it properly. You are dereferencing the pointers to pass to the function which accepts references.


What you are doing is very unnecessary and inefficient though. You don't need any of your pointer stuff at all. Get rid of your pointer variables and then pass the variables in as they are (don't include the "*" to dereference them). The function handles taking in references, you don't have to do anything to "tell it" that you're handing it a reference; it's implied by the function definition.


I wrote this up real quick, it should help: http://pastebin.com/5iL5gvQY

If you need further assistance feel free to message me on the forums here. This forum is directed primarily to questions about SFML and it's associated questions/problems, so try to keep the general C++ questions to a minimum. Some people don't like it around here... :P Anyway like I said, PM me if you have other questions, and good luck!
« Last Edit: November 29, 2012, 04:31:29 am by Ruckamongus »

svladd Cjelik

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: So close ...I just need a little push please :3
« Reply #2 on: November 29, 2012, 05:22:25 am »
Thanks friend.  I got it working like a charm.
 
I honestly thought I had to use pointers because of scope ...alas I did not.  My draw function works perfectly with a simple pass by reference.
 
Again thank you.
 
I shall endevour to heed your advice.

Ruckamongus

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Re: So close ...I just need a little push please :3
« Reply #3 on: November 29, 2012, 06:05:48 am »
Scope is fairly simple: Whatever is inside a curly brace dies at the end of that curly brace.
I'm glad you got it working!

garthos

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: So close ...I just need a little push please :3
« Reply #4 on: November 29, 2012, 09:25:37 pm »
Is correct because you aren't using pointers, you're using references. (They are C++'s way of passing things around. Essentially they are hidden pointers)

lol, when I learn't about pointers and references I worked out they did pretty much the same thing, with references having some restrictions on them. I also learn't that pointers were harder to use. Not being sure which one to use in which case I opted to learn pointers because I could always use a pointer instead of a reference.

So I have found myself in bad habit of only using pointers. I have often read that if a reference is suitable you should use that.

Had my c++ book had mentioned that pointers were the original C way of doings things and references were the C++ way I would have understood the relationship between the two so much better. Thank you for sharing that.

 

anything