【问题标题】:MinGW C++: expected primary-expression before '/' tokenMinGW C++:“/”标记之前的预期主表达式
【发布时间】:2012-02-02 15:35:55
【问题描述】:

这是我在这里的第一个问题,也是我第一次无法通过环顾四周找到在线 C++ 问题的解决方案。我在这方面相对缺乏经验,不确定什么是相关的,所以我会发布我认为可能有用的任何内容。

我正在使用 SDL 制作跨平台应用程序。我在 Windows 7(64 位)上使用 MinGW 4.6.1,以及在另一台计算机上安装 Ubuntu。

它在 Ubuntu(使用 g++)上编译良好,没有任何抱怨,但是当我尝试在我的 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();
};

【问题讨论】:

  • 不喜欢'near'这个名字;如果你把它重命名为真正无害的东西(比如 nr)会发生什么?
  • g++ G:\JibbEngine\003\gameTest.exe G:\JibbEngine\003\gameTest.cpp -lmingw32 -lSDLmain -lSDL -lSDL_image -lOpenGL32 -lglu32 -- 我在 Linux 上以相同的方式编译它,当然,除了使用不同的库,但我的代码的旧版本在两者上都可以工作,所以我不认为这可能是问题。我没有使用类头atm(我知道......),但我无法想象这会让编译器对'/'运算符感到困惑。 .cpp 文件肯定包含在正确的位置并被编译器找到(因此出现错误)。
  • @ScottHunter:哇。谢谢。那解决了它。我可以看到您如何推断这是问题的根源,但是您知道为什么吗? -- 另外,当人们在评论而不是提交的答案列表中找到他们的答案时,通常的做法是什么?
  • @Jibb :迈克的回答解释了原因,并在斯科特发表评论之前发布。 ;-]
  • @ildjarn : 干杯——我将 Mike 的答案打勾为“正确”答案(是否有 stackoverflow 术语可以这样做?),因为它确实解决了问题。

标签: c++ windows g++ mingw32


【解决方案1】:

我的猜测是 near 和/或 far 被定义为宏,可能是为了编译古老的 16 位 DOS/Windows 代码。

尝试在函数前添加#undef near#undef far,看看是否有帮助。

【讨论】:

  • 几乎可以肯定是这样。 nearfar 是 16 位 DOS/Windows 的指针修饰符,它们可能仍在 Windows 标头中的某处定义。
  • 谢谢迈克! @ScottHunter 在上面 cmets 中的解决方案最终成功了,但这也解决了它。
猜你喜欢
  • 2013-08-29
  • 2013-02-23
  • 1970-01-01
  • 1970-01-01
  • 2015-03-31
  • 2012-07-08
  • 2015-01-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多