使用VS直接使用C++存储返回值
嘿,伙计们,我正在写一些代码,我发现了一个奇怪的错误。函数convert_vector2d&i_scale将字符串转换为矢量2D继承自sf::vector2f。如果您检查接下来的几行代码,我将执行相同的操作两次 代码:全选使用VS直接使用C++存储返回值,c++,function,parameters,return-value,C++,Function,Parameters,Return Value,嘿,伙计们,我正在写一些代码,我发现了一个奇怪的错误。函数convert_vector2d&i_scale将字符串转换为矢量2D继承自sf::vector2f。如果您检查接下来的几行代码,我将执行相同的操作两次 代码:全选 float x = convert_Vector2D(&i_scale).x; float y = convert_Vector2D(&i_scale).y; object.SetScale( ( convert_Vector2D(&i_scale)
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();
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;
}
第一次使用convertu vector2d和ss=1,1的返回向量调用setScale。然后,我再次调用object.setScale,这次使用x,y存储的结果,当我调用object.getScale时,我得到ss=1,2,这是预期的/正确的
我遍历了convert函数,它通过两个函数调用返回1,2
代码:全选
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();
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;
不要在堆栈上分配Vector2D,而是在堆上使用new进行分配。您对函数外部temp的引用未定义,很可能是垃圾。这两个SetScale函数是什么样子的?能否向我们展示Vector2f和Vector2D的代码,特别是成员变量和您拥有的任何副本构造函数?我怀疑你们可能在这两种语言中都声明了名为x和y的成员。这确实是真的,x和y在这两种语言中都声明了。为什么这意义重大?我将把复制构造函数添加到vector2d的原始帖子中,sf::vector2f是:我删除了vector2d中的x,y声明,它成功了,无论出于什么原因,在vector2f和vector2d中声明x,y都把它搞砸了。有人想解释一下吗?我正在返回这个值,返回的值我以为我不再引用存储在堆栈上的变量temp了,因为它被返回了?@Ben:你使用得对。我怀疑@user误读了您的代码:非常有外交手腕的乔纳森,我用Java编程太久了,我的大脑线交叉了。。。