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

标签 c++ compiler-errors g++

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

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

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;
}

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

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

此外,我不确定为什么编译器错误会引用“||” token 。 (我对服务器很感兴趣,我按照一些说明将语言环境设置为“C”,这在一定程度上改善了输出,但它看起来仍然不正确。)

谢谢大家

最佳答案

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

比照。 this existing post有关替代 token 的更多详细信息。

关于c++ - Linux g++编译错误: error: expected ',' or '...' before '||' token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17397117/

相关文章:

c++ - 非多态继承对性能/内存使用有影响吗?

c++ - 函数原型(prototype)未初始化的局部变量

c - 将代码从 ATmega32 修改为 Arduino Mega - 函数 `main' 中出现错误,即使没有

c++ - 为什么我的两个 cin 语句没有在程序结束时运行?

c++ - QueryInterface() 成功时可以为我们提供 nullptr 吗?

c++ - 如何在 libpqxx/C++ 准备好的 SQL 语句中将参数绑定(bind)在引号内

C++ 模板偏特化错误

c++ - 对 `Class::Class()' 的 undefined reference

c++ - .begin 左边必须有类/结构/union

compiler-errors - dev86编译错误