我正在尝试为Android构建一些已经在win32上运行的C ++代码。我有一个问题,重载运算符。例如:
代码:
Vector2 uv0 = textures.back()->m_uv0;
Vector2 uvt = textures.back()->m_uvt;
uv0 = m_uv0 + Vector2(uv0.x * m_uvt.x, uv0.y * m_uvt.y) + Vector2(0.01f,0.01f);
Vector2 是上面声明的类。声明是:
class Vector2
{
public:
//Constructors
Vector2() : x(0.0f), y(0.0f){}
Vector2(GLfloat _x, GLfloat _y) : x(_x), y(_y) {}
Vector2(double _x, double _y) : x(static_cast<float>(_x)), y(static_cast<float>(_y)) {}
Vector2(int _x, double _y) : x(static_cast<float>(_x)), y(static_cast<float>(_y)) {}
Vector2(double _x, int _y) : x(static_cast<float>(_x)), y(static_cast<float>(_y)) {}
Vector2(int _x, int _y) : x(static_cast<float>(_x)), y(static_cast<float>(_y)) {}
Vector2(GLfloat * pArg) : x(pArg[0]), y(pArg[1]) {}
Vector2(const Vector2 & vector) : x(vector.x), y(vector.y) {}
//Vector's operations
GLfloat Length();
Vector2 & Normalize();
Vector2 operator + (Vector2 & vector);
Vector2 & operator += (Vector2 & vector);
Vector2 operator - ();
Vector2 operator - (Vector2 & vector);
Vector2 & operator -= (Vector2 & vector);
Vector2 operator * (GLfloat k);
Vector2 & operator *= (GLfloat k);
Vector2 operator / (GLfloat k);
Vector2 & operator /= (GLfloat k);
Vector2 & operator = (Vector2 vector);
Vector2 Modulate(Vector2 & vector);
GLfloat Dot(Vector2 & vector);
void Set(GLfloat _x, GLfloat _y);
//access to elements
GLfloat operator [] (unsigned int idx);
//data members
float x;
float y;
};
这个课程的定义我不会在这里列出,因为它没有得到满足。
但不幸的是我发现了一个错误:
G:/PROJECT266/projects/PROJECT266//jni/../jni/SBE/source/Sprite.cpp: In member function'void Sprite::AddTex(TEX::GUItex)':
G:/PROJECT266/projects/PROJECT266//jni/../jni/SBE/source/Sprite.cpp:103:57: error: no match for 'operator+' in '((Sprite*)this)->Sprite::m_uv0 + Vector2((uv0.Vector2::x *((Sprite*)this)->Sprite::m_uvt.Vector2::x), (uv0.Vector2::y * ((Sprite*)this)->Sprite::m_uvt.Vector2::y))'
G:/PROJECT266/projects/PROJECT266//jni/../jni/SBE/source/Sprite.cpp:103:57: note: candidates are:
G:/PROJECT266/projects/PROJECT266//jni/../jni/SBE/source/SBMath.h:38:10: note: Vector2 Vector2::operator+(Vector2&)
G:/PROJECT266/projects/PROJECT266//jni/../jni/SBE/source/SBMath.h:38:10: note: no known conversion for argument 1 from 'Vector2' to 'Vector2&'
但是,如果我像这样重写上面的代码:
Vector2 uv0 = textures.back()->m_uv0;
Vector2 uvt = textures.back()->m_uvt;
Vector2 vec1 = Vector2(uv0.x * m_uvt.x, uv0.y * m_uvt.y);
Vector2 vec2 = Vector2(0.01f,0.01f);
uv0 = m_uv0 + vec1 + vec2;
编译期间不会出现任何错误。 我无法理解,这个愚蠢错误的原因是什么。 如果你解释我如何解决这个问题,我会很高兴。
答案 0 :(得分:3)
无法将 r值绑定到非const引用。
这一行:
uv0 = m_uv0 + Vector2(uv0.x * m_uvt.x, uv0.y * m_uvt.y) + Vector2(0.01f,0.01f);
相当于:(我用PARAMS
替换参数以使我的示例更具可读性):
uv0 = (m_uv0.operator+(Vector2(PARAMS))).operator+(Vector2(PARAMS));
这里Vector2(PARAMS)
将创建一个临时对象。那就是你试图将r值引用传递给你的运算符重载,并且编译器找不到匹配项,因为你的运算符被声明为:
Vector2 operator+ (Vector2& vector);
有关为什么temporaries无法绑定到非const引用的详细信息,请参阅:How come a non-const reference cannot bind to a temporary object?
在第二个示例中,首先声明两个Vector2
个对象,然后将它们作为 l-value 引用传递给运算符,这些引用与运算符重载相匹配。< / p>
解决问题并让运算符重载同时接受l值和r值引用的一种方法是将其声明为引用const
,因为将r值绑定到对const
的引用。请参阅krsteeve的答案,了解如何做到这一点。
一般情况下,如果您不打算修改参数,则应始终将引用的函数声明为引用const
。
参考绑定示例:
Vector2& ref1 = Vector2(); // Error, trying to bind r-value to non-const ref.
Vector2 v;
Vector2& ref2 = v; // OK, v is an l-value reference.
// It is however OK to bind an r-value to a const reference:
const Vector& ref3 = Vector2(); // OK.
答案 1 :(得分:2)
您尝试将临时对象作为非const引用传递。更改operator +
的签名以获取const引用:
Vector2 operator + (const Vector2 & vector);
您的第二个示例的工作原因是您现在正在命名Vector2对象,它们不再是临时的。