2012-01-04 48 views
2

這是我的第一個問題,也是第一次通過環顧四周無法找到在線C++問題的解決方案。我在這個領域相對缺乏經驗,並且不確定什麼是相關的,所以我只會發布我認爲可能有用的東西。MinGW C++:'/'令牌之前的預期主表達式

我正在使用SDL創建一個跨平臺的應用程序。我在Windows 7(64位)上使用MinGW 4.6.1,以及在另一臺計算機上安裝Ubuntu。

它(使用G ++)編譯Ubuntu的罰款,沒有任何怨言,但我得到以下錯誤,當我嘗試我的Windows機器上編譯G ++:

...matrix.cpp:77:17: error: expected primary-expression before '/' token 
...matrix.cpp:78:11: error: expected primary-expression before '/' token 
...matrix.cpp:79:17: error: expected primary-expression before ')' token 
...matrix.cpp:79:28: error: expected primary-expression before ')' token 
...matrix.cpp:80:19: error: expected primary-expression before ')' token 
...matrix.cpp:80:30: error: expected primary-expression before ')' token 

據我所知,有沒有什麼特別的功能(特別是因爲它編譯罰款在我的Ubuntu的安裝):

Matrix *Matrix::projection(float near, float far, float top, float right) { 
    x1 = near/right; 
    y2 = near/top; 
    z3 = -(far+near)/(far-near); 
    z4 = -(2*far*near)/(far-near); 
    w3 = -1.0; 
    y1 = z1 = w1 = 
    x2 = z2 = w2 = 
    x3 = y3 = 
    x4 = y4 = w4 = 0.0; 
    return this; 
} 

在如此重要的情況下,這裏的Matrix類:

class Matrix { // row-major matrix 
    public: 
     float x1, y1, z1, w1, x2, y2, z2, w2, x3, y3, z3, w3, x4, y4, z4, w4; 
     Matrix(); 
     Matrix (float a, float b, float c, float d, float e, float f, float g, float h, float i, float j, float k, float l, float m, float n, float o, float p); 
     Matrix (float *f); 
     Matrix *identity(); 
     Matrix *translation(float x, float y, float z); 
     Matrix *rotation(float a, float i, float j, float k); 
     Matrix *rotation(Quaternion q); 
     Matrix *projection(float near, float far, float top, float right); 
     Matrix operator*(Matrix m); 
     void operator*=(Matrix m); 
     Matrix operator/(float f); 
     void operator/=(float f); 
     Matrix operator*(float f); 
     void operator*=(float f); 
     void operator=(float *f); 
     float *getArray(); 
}; 
+0

似乎不喜歡'near'這個名字;如果將它重命名爲真正無害的東西(如nr)會怎麼樣? – 2012-01-04 18:29:28

+0

'g ++ G:\ JibbEngine \ 003 \ gameTest.exe G:\ JibbEngine \ 003 \ gameTest.cpp -lmingw32 -lSDLmain -lSDL -lSDL_image -lOpenGL32 -lglu32' - 我用同樣的方式在Linux上編譯它,除了不同圖書館,當然,但我的代碼的舊版本工作,所以我不認爲這可能是一個問題。我沒有使用類頭atm(我知道...),但我無法想象如何讓編譯器對'/'運算符感到困惑。 .cpp文件肯定包含在正確的位置,並且由編譯器找到(因此錯誤)。 – 2012-01-04 18:31:52

+0

@ScottHunter:哇。謝謝。這解決了它。我可以看到你是如何推斷出它是問題的根源的,但你知道爲什麼嗎? - 當人們在評論中找到他們的答案而不是提交答案的列表時,通常的做法是什麼? – 2012-01-04 18:33:30

回答

7

我的猜測是,near和/或far被定義爲宏,可能使古老的16位DOS/Windows代碼可以被編譯。

嘗試在你的函數之前添加#undef near#undef far,看看是否有幫助。

+0

這幾乎是肯定的。 'near'和'far'是針對16位DOS/Windows的指針修飾符,它們可能仍然在Windows頭文件中定義。 – 2012-01-04 18:38:57

+0

謝謝邁克!上述評論中@ ScottHunter的解決方案最終取得了訣竅,但這也解決了這個問題。 – 2012-01-04 18:41:29

相關問題