Class-Return Function

Aug 16, 2011 at 12:24am
I'm having some problems with a class and it's members, I don't know how to return the values.

1
2
3
4
5
6
7
8
9
10
11
12
class CLASS
{
		public:
		void MEMBER(std::string, int);
}
void CLASS::MEMBER(std::string NAME, int AGE)
{
	if (AGE > 18)
	NAME += " is an adult.";
	else
	NAME += " is a child.";
}


I changed NAME, but that doesn't change the value the user sent originally.

1
2
3
4
CLASS Shay9999;
string myname = "Stan Marsh";
Shay9999.MEMBER(myname, 15);
cout<<myname;


When I add a return to it
1
2
3
...else
NAME += " is a child.";
return (NAME);


I get an error that Void is trying to return a value.
Last edited on Aug 16, 2011 at 12:40am
Aug 16, 2011 at 1:41am
Change the arguments from (std::string, int) to (std::string&, int&)

The '&' is just a pointer that automatically gets dereferenced when you use it, rather than you typing '*' before every pointer to access its value.

Why don't you just add name/age to the class though?
Aug 16, 2011 at 2:13am
I just used them as an example =P and because I'm using a library called SFML, and I want to be able to enter an empty sprite and after it runs through, it becomes filled with an image.

Now that I added the '&' (xD), I'm having some problems altering images. It seems to get a white screen where the image would normally be located. Should I be asking this on the SFML forums, or does somebody here know (^_^)?
Aug 16, 2011 at 6:35pm
The code you have posted has absolutely nothing to do with SFML, or images either. Please post the code you believe is causing the problem (perhaps the code that does stuff with your image...).
Aug 17, 2011 at 4:02am
Here's the SFML variant.... (Is that the word to use? Variant? I don't know, I think I should using something else)


class CLASS
{
public:
void MEMBER(std::string, sf::Sprite&);
};

void KonGraphs::MEMBER(std::string FILE, sf::Sprite& SPRITE)
{
sf::Image Image;
Image.LoadFromFile(FILE);
SPRITE.SetImage(Image);
}
Last edited on Aug 17, 2011 at 4:03am
Aug 17, 2011 at 10:45am
http://www.sfml-dev.org/tutorials/1.6/graphics-sprite.php
Images and sprites management wrote:
a sprite only points to an external image it doesn't own one
Aug 17, 2011 at 8:43pm
Thanks? I guess =P. No really, though, thanks for the help ^_^
Topic archived. No new replies allowed.