хранение возвращаемых значений с последующим использованием vs с использованием непосредственно c ++ - PullRequest
0 голосов
/ 24 сентября 2010

Эй, ребята, я писал какой-то код и обнаружил странную ошибку. Функция convert_vector2d (& i_scale) преобразует строку в vector2d (наследуется от sf :: vector2f). Если вы изучите следующие несколько строк кода, я делаю одну и ту же операцию дважды.

Код: Выбрать все

float x = convert_Vector2D(&i_scale).x;
float y = convert_Vector2D(&i_scale).y;
object.SetScale( ( convert_Vector2D(&i_scale) ) );
ss = object.GetScale();
object.SetScale( x , y );
ss = object.GetScale();

Первый раз я вызываю setScale с вектором возврата от convert_vector2d и ss = 1,1. Затем я снова вызываю object.setScale с x, y (сохраненные результаты), и когда я вызываю object.getScale, я получаю ss = 1,2 (что является ожидаемым / правильным).

Я прошел через функцию преобразования, и она возвращает 1,2 через оба вызова функции.

Код: Выбрать все

const Vector2D Map::convert_Vector2D(std::string * string_to_convert)
{
    size_t foundit = 0;
    Vector2D temp;
    std::string one, two;
    if( (foundit = string_to_convert->find(',')) != std::string::npos &&
        string_to_convert->find_first_of(',') == string_to_convert->find_last_of(',') ) // only one comma per line.
    {
        one = string_to_convert->substr(0, foundit);
        two = string_to_convert->substr(foundit+1, string_to_convert->size()); // +1 to skip over the comma.

        temp.x = (float)strtod( one.c_str(), NULL );
        temp.y = (float)strtod( two.c_str(), NULL );

        check_conversion_errors_vector2d(temp, string_to_convert);
    }
    else
    {
        Debugger::print("MapLoader: Error: more then one comma on line %d of file %s. Stopping reading of file.\n",
            i_Current_Line, mMapName.c_str() );
        i_QuitParsing = true; // TODO: maybe add return after this line?
    }

    return temp;
}

Есть идеи, почему у меня другое поведение?

void Drawable::SetScale(float ScaleX, float ScaleY)
{
    SetScaleX(ScaleX);
    SetScaleY(ScaleY);
}

void Drawable::SetScale(const Vector2f& Scale)
{
    SetScaleX(Scale.x);
    SetScaleY(Scale.y);
}

void Drawable::SetScaleX(float FactorX)
{
    if (FactorX > 0)
     {
          myScale.x       = FactorX;
          myNeedUpdate    = true;
          myInvNeedUpdate = true;
     }
}


void Drawable::SetScaleY(float FactorY)
{
     if (FactorY > 0)
     {
          myScale.y = FactorY;
          myNeedUpdate    = true;
          myInvNeedUpdate = true;
     }
 }

Конструкторы копирования SFML и переменные-члены:

// = equals operator assignment
Vector2D& operator=(const Vector2D &rhs)
{
    if(this == &rhs)
    {
        return *this;
    }
    else
    {
        this->x = rhs.x;
        this->y = rhs.y;
        return *this;
    }
}
// = equals operator assignment
Vector2D& operator=(const sf::Vector2f &rhs)
{
    this->x = rhs.x;
    this->y = rhs.y;
    return *this;
}

float x, y;

1 Ответ

0 голосов
/ 24 сентября 2010

Не выделяйте Vector2D в стеке, делайте это в куче с новым.Ваша ссылка на temp вне функции не определена, вероятно, это мусор.

...