【问题标题】:Linux g++ compile error: error: expected ',' or '...' before '||' tokenLinux g++ 编译错误:错误:在 '||' 之前需要 ',' 或 '...'令牌
【发布时间】:2013-07-01 03:39:34
【问题描述】:

首先让我说这在 Visual Studio 中编译和运行良好。但是当我在 Linux (g++) 上编译相同的文件时,我得到了声明和实现 << 运算符重载的编译错误。

代码的相关部分摘录如下。 (它是一个 .cpp 文件,其中包含 Google 测试用例,并且散布了类和方法定义以支持测试用例。)除了代码的相关部分之外,我省略了所有代码(我希望如此)。

class orderrequest : public msg_adapter {
public:
    // ... snip

    friend bool operator ==(const orderrequest &or1, const orderrequest &or2);
    friend ostream& operator <<(ostream &out, const orderrequest &or);  // compiler error here
};

bool operator ==(const orderrequest &or1, const orderrequest &or2) {
    bool result = or1.symbol == or2.symbol
        && or1.orderQty == or2.orderQty;

    // ...  snip
    return result;

}

// compiler error here
ostream& operator <<(ostream &out, const orderrequest &or) {

    out << "symbol=" << or.symbol << ",orderQty=" << or.orderQty;
    return out;
}

编译会抛出一些错误,似乎都与尝试重载 &lt;&lt; 运算符有关:

EZXMsgTest.cpp:400: error: expected ',' or '...' before '||' token
EZXMsgTest.cpp:428: error: expected ',' or '...' before '||' token
EZXMsgTest.cpp: In function 'std::ostream& operator<<(std::ostream&, const orderrequest&)':
EZXMsgTest.cpp:430: error: expected primary-expression before '||' token
EZXMsgTest.cpp:430: error: expected primary-expression before '.' token
EZXMsgTest.cpp:430: error: expected primary-expression before '||' token
EZXMsgTest.cpp:430: error: expected primary-expression before '.' token

第 400 行是 friend ostream&amp; operator &lt;&lt; 行,第 430 行是 &lt;&lt; 运算符的方法实现。

另外,我不确定为什么编译器错误引用了“||”令牌。 (我被绑定到服务器,我按照一些说明将语言环境设置为“C”,这在一定程度上改善了输出,但看起来仍然不正确。)

谢谢大家。

【问题讨论】:

    标签: c++ compiler-errors g++


    【解决方案1】:

    or 在 C++ 中保留 (§2.12/2 C++11)。它是|| (§2.6/2) 的替代标记,因此您不能将其用作标识符。将变量从 or 重命名为其他名称以解决此问题。

    参照。 this existing post 了解有关替代令牌的更多详细信息。

    【讨论】:

    • 谢谢!我认为这可能与使用“或”有关(我已经通过将类从“或”重命名为“orderrequest”),但我从未真正想过或者是 C++ 使用的标记。您为我节省了更多修改更改的时间!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-16
    • 1970-01-01
    相关资源
    最近更新 更多