2013-07-01 91 views
3

让我开始说这个编译并在Visual Studio中运行良好。但是,当我在Linux(g ++)上编译相同的文件时,我得到编译错误来声明和实现运算符的过载。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; 
} 

的编译抛出了一些错误,所有看似相关的努力超载<<操作:

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”,这有助于提高输出,但它看起来仍然不正确。)

谢谢大家。

回答

6

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

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

+1

谢谢!我想也许它是与“或”的使用有关(我已经通过将类从“或”改名为“orderrequest”),但我从来没有真正想过或是C++使用的令牌。你为我节省了更多的时间! –

相关问题