矩阵类:错误:")"标记之前的预期主表达式

Matrix Class: error: expected primary-expression before ')' token

本文关键字:表达式 错误      更新时间:2023-10-16

我有一个Matrix4类派生/扩展矩阵基类(模板类)。模板类的方法在同一个头文件中声明和定义。

我只复制了给出错误的"Matrix4"类的部分。同样的错误出现在第10行&13. 我看不到任何缺失的变量或参数。我试过去掉括号,但是没有用。

我已经搜索了一个线索,我可能做错了什么,但我没有发现任何有用的类似问题在这个网站上…我真的很感激你的帮助。

给出错误的Matrix4类:
template<typename T>
class Matrix4 : public Matrix<T, 4>
{
public:
    Matrix4() { }
    inline Matrix4 InitOrthographic(T left, T right, T bottom, T top, T near, T far)
    {
        const T width = (right - left);
        const T height = (top - bottom);
        const T depth = (far - near);  //error occurs here
        (*this)[0][0] = T(2)/width; (*this)[1][0] = T(0);        (*this)[2][0] = T(0);        (*this)[3][0] = -(right + left)/width;
        (*this)[0][1] = T(0);       (*this)[1][1] = T(2)/height; (*this)[2][1] = T(0);        (*this)[3][1] = -(top + bottom)/height;
        (*this)[0][2] = T(0);       (*this)[1][2] = T(0);        (*this)[2][2] = T(-2)/depth; (*this)[3][2] = -(far + near)/depth;  //and here
        (*this)[0][3] = T(0);       (*this)[1][3] = T(0);        (*this)[2][3] = T(0);        (*this)[3][3] = T(1);  
        return *this;
    }

Base Matrix类:

template<typename T, unsigned int D>
class Matrix
{
    public:
        Matrix() { }
        virtual ~Matrix() { }
        Matrix(const Matrix& other) { *this = other; }
        inline Matrix InitIdentity();   //defined in the same header, but left out here to save space
        inline Matrix InitTranslation(const Vector<T, D-1>& r);
        inline Matrix& operator=(const Matrix& rhs);
        inline Matrix operator*(const Matrix<T,D>& r) const;
        inline const T* operator[](int index) const { return m[index]; }
        inline T* operator[](int index) { return m[index]; }
    private:
        T m[D][D];
};

在基类"Matrix"中没有错误,只有在派生类"Matrix4"中有错误。

如果代码看起来很熟悉,那是因为我正在学习Youtube教程

找到解决方案了!谢谢你的帮助!

我把我的参数变量名从"far" &"near"到"m_near" &"m_far",现在它工作了。我认为这可能是与代码中某个类中的#define或其他方法冲突。

编译和运行没有错误。我找不到导致冲突/错误的原始问题的原因。改变似乎已经解决了这个问题,所以我不认为需要太长时间或努力。

const,在方法内的变量声明之前,似乎还没有产生任何不希望的效果,所以我将保留它。

修复了Matrix4类的代码:

inline Matrix4<T> InitOrthographic(T left, T right, T m_near, T m_far, T bottom, T top)
{
    const T width = right - left;
    const T height = top - bottom;
    const T depth = m_far - m_near;  
    (*this)[0][0] = T(2)/width; (*this)[1][0] = T(0);        (*this)[2][0] = T(0);        (*this)[3][0] = -(right + left)/width;
    (*this)[0][1] = T(0);       (*this)[1][1] = T(2)/height; (*this)[2][1] = T(0);        (*this)[3][1] = -(top + bottom)/height;
    (*this)[0][2] = T(0);       (*this)[1][2] = T(0);        (*this)[2][2] = T(-2)/depth; (*this)[3][2] = -(m_far + m_near)/depth;
    (*this)[0][3] = T(0);       (*this)[1][3] = T(0);        (*this)[2][3] = T(0);        (*this)[3][3] = T(1);
    return *this;
}

试一下,它在MS Visual c++下编译成功

template<typename T, unsigned int D>
class Matrix
{
    public:
        Matrix() { }
        virtual ~Matrix() { }
        Matrix(const Matrix<T, D>& other) { *this = other; }
        inline Matrix<T, D> InitIdentity();   //defined in the same header, but left out here to save space
        inline Matrix<T, D> InitTranslation(const Vector<T, D-1>& r);
        inline Matrix<T, D>& operator=(const Matrix<T, D>& rhs);
        inline Matrix<T, D> operator*(const Matrix<T,D>& r) const;
        inline const T* operator[](int index) const { return m[index]; }
        inline T* operator[](int index) { return m[index]; }
    private:
        T m[D][D];
};
template<typename T>
class Matrix4 : public Matrix<T, 4>
{
public:
    Matrix4() { }
    inline Matrix4<T> InitOrthographic(T left, T right, T bottom, T top, T near, T far)
    {
        const T width = (right - left);
        const T height = (top - bottom);
        const T depth = (far - near);  //error occurs here
        (*this)[0][0] = T(2)/width; (*this)[1][0] = T(0);        (*this)[2][0] = T(0);        (*this)[3][0] = -(right + left)/width;
        (*this)[0][1] = T(0);       (*this)[1][1] = T(2)/height; (*this)[2][1] = T(0);        (*this)[3][1] = -(top + bottom)/height;
        (*this)[0][2] = T(0);       (*this)[1][2] = T(0);        (*this)[2][2] = T(-2)/depth; (*this)[3][2] = -(far + near)/depth;
        (*this)[0][3] = T(0);       (*this)[1][3] = T(0);        (*this)[2][3] = T(0);        (*this)[3][3] = T(1);  //and here
        return *this;
    }
};

我不知道Vector类发生了什么,因为我用int

代替了它