2015-10-17 101 views
4

我有从/扩展矩阵基类(模板类)派生的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; 
    } 

基本基质类:

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]; 
}; 

有在底部没有错误“黑客帝国”级,仅在派生“Matrix4”级。

如果因为我下一个YouTube教程的xD

+0

有一个在上面的代码中没有行号。所以10和13是没有意义的。哪一行导致错误?我没有任何错误... – knightrider

+0

此代码充满了在类方法中返回'Matrix'的错误示例。 – Mykola

+1

我添加了注释以指示发生错误的位置。例如_const T depth =(far-near); _ line和_(* this)[0] [3] = T(0); (* this)[1] [3] = T(0); (* this)[2] [3] = T(0); (* this)[3] [3] = T(1); //和here_ – searchnot

回答

1

试试这个代码看起来很熟悉了,它在微软的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代替它

+1

xD它看起来像完全一样的代码?做了什么改变?我正在使用Code :: Blocks IDE – searchnot

+0

编译mingw并不完全,您会回避错误类型,仔细查看答案。 – Mykola

+0

在模板类中,你必须返回一个模板对象 – Mykola

2

找到解决方案!谢谢您的帮助!

我改变了我的说法的变量名来自“远” & “近”到_ “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; 
} 
+0

“远”和“近”通常用于装饰某些奇怪计算机(MSVC用于锁定目标)的指针类型,以便它们可能位于被解析。严格的C++说他们不是关键字,所以你的代码应该被认为是有效的,无论如何。 – dascandy

+0

我想我可能有一个变量或#define代码中的其他地方,但我找不到任何东西。 @Mykola说他成功编译了MS Visual C++,所以我认为它可能是编译器,或者简单地说IDE如何进入命令行 – searchnot

+0

向后兼容性ftw。 –

相关问题