Linux g++ 编译错误:错误:预期的"",""或"..."在"||"标记之前

Linux g++ compile error: error: expected ',' or '...' before '||' token

本文关键字:错误 Linux 编译 g++      更新时间:2023-10-16

让我首先说一下,这在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;
}

编译抛出了一些错误,所有这些似乎都与试图重载<<运算符有关:

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& operator <<行,行430是<<运算符的方法实现。

此外,我不确定编译器错误引用"||"标记的原因。(我被推到服务器上,按照一些说明将区域设置为"C",这在一定程度上提高了输出,但看起来仍然不太好。)

谢谢大家。

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

参见。这篇现有的帖子了解关于替代代币的更多细节。