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

Author Topic: Function returning wrong value  (Read 1964 times)

0 Members and 1 Guest are viewing this topic.

cristi121

  • Newbie
  • *
  • Posts: 33
    • View Profile
Function returning wrong value
« on: April 14, 2011, 09:17:19 pm »
Hi!

I am trying to make a Pong game, but I'm having some trouble with a function.

Code: [Select]

int Screen_1::Run (sf::RenderWindow &app)
{
    Menu menu;

    // If i put return 1 here, it works;

    menu.addWidget ("Media/play.png", 330, 300, 150, 50);

    // If I put it after I use the menu object, it doesn't work anymore;
    // It returns a very large number, aprox. 24 milion
    // Interestingly, it's always the same number

    menu.addWidget ("Media/exit.png", 330, 380, 150, 50);

    menu.addWidget ("Media/title.png", 250, 80, 300, 80);
   
    ...
       
}


Is it possible for an operation on the menu to affect the behaviour of the return statement?
The operations on the menu do not affect any variable in the function, except for app.

Thanks.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Function returning wrong value
« Reply #1 on: April 14, 2011, 10:07:40 pm »
This is possible, and it means that you evoke undefined behavior. I.e. a bug that must be eliminated. As long as you don't find it, you cannot know what your application does, it may wreak havoc at any time.

The error is probably inside addWidget(), but it may also occur earlier. Perhaps you see it when checking this function (can you show it?), otherwise try to minimize the problem to a small, complete code that still reproduces the behavior.

Also check if you link everything correctly, and don't mix release/debug configurations. But first, check again if the return value is really really not 1 (use cout) :D
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

cristi121

  • Newbie
  • *
  • Posts: 33
    • View Profile
Function returning wrong value
« Reply #2 on: April 14, 2011, 11:02:22 pm »
Code: [Select]

void Menu::addWidget (std::string file, int x = 0, int y = 0, unsigned length = 0, unsigned width = 0)
{
    ++nr;

    widget[nr].setImage (file);

    widget[nr].setPosition (x, y);

    if ( length && width)
            widget[nr].setSize (length, width);
}


void Widget::setImage(std::string file)
{
    image.LoadFromFile (file);

    sprite.SetImage (image);

    x = 0;

    y = 0;

    length = sprite.GetSize().x;

    width = sprite.GetSize().y;
}


void Widget::setSize (int newLength, int newWidth)
{
    length = newLength;

    width = newWidth;
}


void Widget::setPosition (int newX, int newY)
{
    x = newX;

    y = newY;
}


Thanks for you answer.

I had already made sure that I am returning the right thing.

But what I don't understand is how code from the menu can affect the value the function returns.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Function returning wrong value
« Reply #3 on: April 14, 2011, 11:09:16 pm »
Quote from: "cristi121"
But what I don't understand is how code from the menu can affect the value the function returns.
Well, as I said, the results of undefined behavior are just undefined. Everything can happen. This is a term the C++ standard uses for situations which are not covered by the language rules and which needn't be considered by compiler writers.

On the quick, I don't see any mistakes in your code. Are the array indices always correct? Use std::tr1::array or std::vector instead of raw arrays, these classes check indices in debug mode.

Otherwise, as stated: Reduce the code to a minimal and complete example that reproduces the problem.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

cristi121

  • Newbie
  • *
  • Posts: 33
    • View Profile
Function returning wrong value
« Reply #4 on: April 15, 2011, 08:34:36 am »
I switched to a vector instead of array and this solved my problem.
Thanks for your help.  :mrgreen: