3

我正在尝试为 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;

在编译过程中不会有任何错误。我不明白,这个愚蠢的错误的原因是什么。如果您向我解释如何解决此问题,我将非常高兴。

4

2 回答 2

3

无法将r 值绑定到非常量引用

这一行:

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);

有关为什么临时对象不能绑定到非常量引用的更多信息,请参阅:How come a non-const reference cannot bind to a temporary object?

在第二个示例中,您首先声明两个Vector2对象,然后将它们作为与您的运算符重载匹配的左引用传递给运算符。

解决此问题并让运算符重载同时采用左值和右值引用的一种方法是将其声明为采用对的引用,const因为将右值绑定到对的引用非常好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.
于 2013-08-25T22:34:41.167 回答
2

您正在尝试将临时对象作为非常量引用传递。更改您的签名operator +以获取 const 引用:

Vector2 operator + (const Vector2 & vector);

您的第二个示例有效的原因是您现在正在命名 Vector2 对象,它们不再是临时的。

于 2013-08-25T21:35:50.293 回答