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
booloperator< (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;
returnfalse;
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?
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.
booloperator< (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;
returnfalse;
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!=