2015-10-17 99 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。 –

相關問題