Sam Goldberg
Sam Goldberg

Reputation: 6801

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

Let me start by saying that this compiles and runs fine in Visual Studio. But when I compile the same file on Linux (g++), I get compile errors for declaration and implementation of overload of the << operator.

The relevant part of the code is extracted below. (It is a .cpp file with Google Test cases in it, and has class and method definitions sprinkled through to support the test cases.) I elided all but the relevant parts of the code (I hope).

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

The compile throws a few errors, all seemingly related to trying to overload the << 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

Line 400 is the friend ostream& operator << line, and line 430 is the method implementation for the << operator.

Also, I'm not sure why the compiler error references the "||" token. (I am puttied to server, and I followed some instructions to set locale to "C" which improved output somewhat, but it still doesn't look right.)

Thanks everyone.

Upvotes: 3

Views: 1093

Answers (1)

jogojapan
jogojapan

Reputation: 69997

or is reserved in C++ (§2.12/2 C++11). It's an alternative token for || (§2.6/2), so you can't use it for an identifier. Rename the variable from or to something else to solve this problem.

Cf. this existing post for further details on alternative tokens.

Upvotes: 6

Related Questions