Linux g++编译错误:error: expected ',' or '...' before '||' token。

3

首先,让我说一句,在Visual Studio中,这个程序可以编译并运行得很好。但是在Linux(g++)上编译相同的文件时,声明和实现重载<<运算符出现了编译错误。

下面是代码的相关部分。(这是一个.cpp文件,其中包含Google Test案例,还有散布的类和方法定义来支持测试案例。)我省略了除相关部分以外的所有内容(希望如此)。

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”,这样输出就有所改善,但仍然不正确。)

谢谢大家。

1个回答

6
or在C++(§2.12/2 C++11)中是保留字。它是||(§2.6/2)的备选符号,因此您不能将其用作标识符。将变量从or重命名为其他名称即可解决此问题。
更多关于备选符号的详细信息,请参见此现有帖子

1
谢谢!我以为可能与使用“or”有关(我已经重命名了类从“or”到“orderrequest”),但我从来没有真正想过“or”是C++中使用的一个标记。你节省了我更多时间进行修改的时间! - Sam Goldberg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接