gpt4 book ai didi

C++ 编译错误,运算符不匹配

转载 作者:太空狗 更新时间:2023-10-29 20:42:27 25 4
gpt4 key购买 nike

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

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

最佳答案

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

这一行:

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) 将创建一个临时对象。也就是说,您正在尝试将右值引用传递给您的运算符重载,并且编译器将找不到匹配项,因为您的运算符声明为:

Vector2 operator+ (Vector2& vector);

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

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

解决此问题并让运算符重载同时获取左值和右值引用的一种方法是将其声明为对 const 的引用,因为绑定(bind) r 是完全没问题的-对 const 的引用的值。查看krsteeve的回答了解如何做到这一点。

一般而言,如果您不打算修改参数,则应始终将采用引用的函数声明为采用对 const 的引用。

引用绑定(bind)示例:

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.

关于C++ 编译错误,运算符不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18433714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com