Help! Fixing missing ")"

My professor gave me the following lines to work with in a CPP for a header file:

1
2
3
4
5
6
7
8
bool operator< (const GroceryItem & lhs, const GroceryItem & rhs)
{
	if ( auto result = lhs._upc         .compare( rhs._upc       ); result != 0) return result < 0;
	if ( auto result = lhs._productname .compare( rhs.productName); result != 0) return result < 0;
	if ( auto result = lhs._brandname   .compare( rhs._brandname ); result != 0) return result < 0;
	if ( std::abs(lhs._price - rhs._price) >= EPSILON) return lhs._price < rhs._price;

	return false;

For some reason or another that i can't figure out there is an error with the semicolons after the closing parenthesis before the "result"
The error being " Expected a ')'.
Any help?
Last edited on
You are missing a bracket at the end. Adding that ' } ' I get not the error " Expected a ')'" , but the following errors:

[bcc32c Error] main.cpp(11): unknown type name 'GroceryItem'
[bcc32c Error] main.cpp(11): unknown type name 'GroceryItem'
[bcc32c Error] main.cpp(16): use of undeclared identifier 'EPSILON'

which are natural since i don't have them defined and declared.
Last edited on
You need to remove ; result:
1
2
3
4
5
6
7
8
bool operator< (const GroceryItem & lhs, const GroceryItem & rhs)
{
	if ( auto result = lhs._upc         .compare( rhs._upc       ); result != 0) return result < 0;
	if ( auto result = lhs._productname .compare( rhs.productName); result != 0) return result < 0;
	if ( auto result = lhs._brandname   .compare( rhs._brandname ); result != 0) return result < 0;
	if ( std::abs(lhs._price - rhs._price) >= EPSILON) return lhs._price < rhs._price;

	return false;
Further more you need () in order to get the result of the compare function:

if ( (auto result = lhs._upc .compare( rhs._upc ) ) != 0) return result < 0; // Note the additional ()

The reason is the higher precedence of the operator!=

See:

http://www.cplusplus.com/doc/tutorial/operators/
@coder777: see https://en.cppreference.com/w/cpp/language/if (If Statements with Initializer)

@OP: you need c++17
Registered users can post here. Sign in or register to post.