Returning multiple values from function C++

Started by
10 comments, last by Khatharr 11 years, 5 months ago
Hey i have a function which i use to preload images, but its a bit messy.


I'm trying to load images and return them so then my class can load the images. But I'm complete confused how it works.


This is my function:



[source lang="cpp"]void menuPreload(std::string theme,std::string theme_on,sf::Texture &button, sf::Texture &button_on,int &width,int &height){
std::string img;
std::string img_on;
img = theme;
img_on = theme_on;

if (!button.loadFromFile(img)){
exit(4);
}

if (!button_on.loadFromFile(img_on)){
exit(5);
}

sf::Sprite sprite(button);
width = sprite.getLocalBounds().width;
height = sprite.getLocalBounds().height;
}[/source]

I think call it like this in my main function:

[source lang="cpp"] sf::Texture button;
sf::Texture button_on;
int width;
int height;
menuPreload(config["Button"],config["Button_On"],button,button_on,width,height); [/source]


After i have preloaded the images i call my class to load the button like this:

[source long="cpp"]
//before loop
Button btn_quit(window.getSize().y/2, 200,width,height);

//inside my loop
btn_quit.RenderBttn(window,mouseX,mouseY,button,button_on);
[/source]


So far i think this is correct... but then in my class i have:


[source lang="cpp"]class Button
{
private:
int m_x, m_y;
int m_w, m_h;

public:
Button(int x, int y, int m_w, int m_h)
{
m_y = y;
m_x = x - m_w/2;
}

bool IsIn( int mouseX, int mouseY )
{
exit(m_h);

if (((mouseX > m_x) && (mouseX < m_x + m_w))
&& ((mouseY > m_y) && (mouseY < m_y + m_h ) ) ) {
return true;
} else {
return false;
}
}

void RenderBttn(sf::RenderWindow &destination,int mouseX, int mouseY,sf::Texture button, sf::Texture button_on)
{

sf::Sprite result(IsIn(mouseX,mouseY) ? button_on : button);
result.setPosition( m_x , m_y);
destination.draw(result);

}
};[/source]

The issue is when the mouse is over the button it doesn't change, m_w seems to be not set or incorrect. Am i on the right lines here... my brain is about to explode ... tongue.png
Advertisement
Just a hunch...

Perhaps you mean to do this:

void RenderBttn(sf::RenderWindow &destination,int mouseX, int mouseY,[color=#0000ff]sf::Texture &button, sf::Texture &button_on)
{

sf::Sprite result(IsIn(mouseX,mouseY) ? button_on : button);
result.setPosition( m_x , m_y);
destination.draw(result);
}

Instead of this:

void RenderBttn(sf::RenderWindow &destination,int mouseX, int mouseY,[color=#ff0000]sf::Texture button, sf::Texture button_on)
{

sf::Sprite result(IsIn(mouseX,mouseY) ? button_on : button);
result.setPosition( m_x , m_y);
destination.draw(result);
}
Hmm i seem to get this happening :

SFML.exe: Native' has exited with code -858993460 (0xcccccccc).exe

I have no idea what that means to dissect the problem
You have a call to exit in your code for some reason: "exit(m_h)". I would advise googling for 0xcccccccc, such patterns are usually indicative of the compiler or runtime setting memory to particular values, trying to flush out bugs for you.
I had that to check the value of m_h to make sure it was correct. I tend to exit with the variable if they are ints to check they are correct :)

I removed that exit now.

I've just re-done my header files. And moved the functions to a different cpp file so i can follow it better. It got rid of the 0xcccccccc error but i got this :




1> menu.cpp
1>desktop\c++\projects\sfml\sfml\menu.cpp(19): warning C4244: '=' : conversion from 'float' to 'int', possible loss of data
1>desktop\c++\projects\sfml\sfml\menu.cpp(20): warning C4244: '=' : conversion from 'float' to 'int', possible loss of data
1>desktop\c++\projects\sfml\sfml\menu.cpp(53): warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
1>desktop\c++\projects\sfml\sfml\menu.cpp(53): warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
1> main.cpp
1>desktop\c++\sfml\sfml-2.0\include\sfml\window\window.hpp(476): error C2248: 'sf::NonCopyable::NonCopyable' : cannot access private member declared in class 'sf::NonCopyable'
1>desktop\c++\sfml\sfml-2.0\include\sfml\system\noncopyable.hpp(67) : see declaration of 'sf::NonCopyable::NonCopyable'
1>desktop\c++\sfml\sfml-2.0\include\sfml\system\noncopyable.hpp(42) : see declaration of 'sf::NonCopyable'
1> This diagnostic occurred in the compiler generated function 'sf::Window::Window(const sf::Window &amp;)'
1>desktop\c++\sfml\sfml-2.0\include\sfml\graphics\rendertarget.hpp(369): error C2248: 'sf::NonCopyable::NonCopyable' : cannot access private member declared in class 'sf::NonCopyable'
1>desktop\c++\sfml\sfml-2.0\include\sfml\system\noncopyable.hpp(67) : see declaration of 'sf::NonCopyable::NonCopyable'
1>desktop\c++\sfml\sfml-2.0\include\sfml\system\noncopyable.hpp(42) : see declaration of 'sf::NonCopyable'
1> This diagnostic occurred in the compiler generated function 'sf::RenderTarget::RenderTarget(const sf::RenderTarget &amp;)'
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
[/quote]
You are probably passing the render window to a function as a copy instead of reference. Render Window inherits NonCopyable so you can't do that.
Yeh you were correct i forgot the &

Thanks :)

I tend to exit with the variable if they are ints to check they are correct
[/quote]
A better way to do this is to use assertions. In C++, you can include <cassert> and write code like this:

assert(someVariable == 42);

The nice thing about assertions is they are only compiled in debug builds. When you build for release, these checks will be removed. Also depending on your toolchain they can have good integration with your IDE. For instance, the assertions in Visual Studio allow you to break into the debugger, which allows you to examine the call stack and discover what led to the erroneous value.

It also means that if the value is correct your program will continue to run!
If you're using a bit more modern C++, you could use std::tuple for return value and std::tie to unpack it, for example:

[source lang="cpp"]#include <tuple>

std::tuple<int, float> foo()
{
return std::make_tuple(1, 2.0f);
}

int main()
{
int a;
float b;

std::tie(a, b) = foo();
}
[/source]

Where are we and when are we and who are we?
How many people in how many places at how many times?
it should be noted that std::tie has some MAJOR performance penalties if you aren't careful. We had some serious issues with boost / std::tie and using that to return multiple values, since you hav ea LOT of possible value-copying going on (I took a co-worker to town for this; it KILLED our application's throughput because of the overhead for both tie creation and access).

In my limited experience, this is the result of people who aren't used to the idea of the entre value being copied (ie, they are just used to references or pointers being copied).
In this case, i believe the OP's problem was solved by using & to make sure references were passed in.

In general, although this depends greatly on your development environment, you may want to consider passing such values as pointers, and non-modifiable references as const XXX&. this will give the programmer (most probably yourself) a clearer understanding of what they can expect to happen in the function.

This topic is closed to new replies.

Advertisement