没有运算符"[]"匹配这些操作数/没有模板化代码

No operator "[]" matches these operands / no templated code

本文关键字:代码 操作数 运算符      更新时间:2023-10-16

im试图在此问题中修复示例代码:如何将3D点转换为2D透视投影?

我已经解决了大多数错误,但是一个人让我感到困惑。

有一个class矩阵,带有[]运算符

float& operator[]( size_t index )
{
    if (index >= 16) {
        std::out_of_range e( "" );
        throw e;
    }
    return data[index];
}

我在此代码上获得了一个非匹配操作员错误:

inline Vector operator*( const Vector& v, const Matrix& m )
{
    Vector dst;
    dst.x = v.x*m[0] + v.y*m[4] + v.z*m[8] + v.w*m[12];
    dst.y = v.x*m[1] + v.y*m[5] + v.z*m[9] + v.w*m[13];
    dst.z = v.x*m[2] + v.y*m[6] + v.z*m[10] + v.w*m[14];
    dst.w = v.x*m[3] + v.y*m[7] + v.z*m[11] + v.w*m[15];
    return dst;
}

我无法弄清楚操作员定义有什么问题。

在提出问题后,精确30秒,它遇到了我的operator*正在接收 const 矩阵对象。

问题是operator[]不是const函数。

在此处留在这里,因为Google上只有两个不同的问题 -