Quinton Wells
Quinton Wells

Reputation: 31

Macros Expected an Expression

I'm currently working on a school project and the requirements for part of the assignment I am working on looks like this:

The error state which the client can reference to determine if the object holds a valid date, and if not, which part of the date is in error. The possible error states are integer values defined as macros in the Date class header:

NO_ERROR   0  -- No error - the date is valid
CIN_FAILED 1  -- istream failed on information entry
YEAR_ERROR 2  -- Year value is invalid
MON_ERROR  3  -- Month value is invalid
DAY_ERROR  4  -- Day value is invalid"

And:

void errCode(int errorCode);

This function sets the error state variable to one of the values listed above.

However when I put my code into the second function visual studio gives me a red underline and states "expected an expression" when I try to use the macro values in an if statement.

// this code is unfinished and partial

void Date::errCode(int errorCode)
{
    if (errorCode == NO_ERROR) // <- red underline here
    {
        m_errorState = NO_ERROR; // <- red underline here
    }
    if (errorCode == 1)
    {
    }
}

The header file defines look like this:

#define NO_ERROR = 0        // No error - the date is valid
#define CIN_FAILED = 1      // istream failed on information entry
#define YEAR_ERROR = 2      // Year value is invalid
#define MON_ERROR = 3       // Month value is invalid
#define DAY_ERROR = 4       // Day value is invalid

Upvotes: 2

Views: 4331

Answers (2)

user0042
user0042

Reputation: 8018

In c++ you don't want to use macros whenever you can avoid them. That's primarily because c-preprocessor macros are merely text replacement and leads to unexpected code results for the unexperienced (or unaware) reader of your code.

The wrong macro defintions you have, lead to text expansion like

if (errorCode == = 0)
              // ^^^

which of course leads to a syntax error.


The correct way to do that in c++ is to use a enum class declared inside of your Date class (struct):

class Date {
public:
    enum class ErrorCodes {
         NO_ERROR = 0        // No error - the date is valid
       , CIN_FAILED = 1      // istream failed on information entry
       , YEAR_ERROR = 2      // Year value is invalid
       , MON_ERROR = 3       // Month value is invalid
       , DAY_ERROR = 4       // Day value is invalid
    };
private:
    void errCode();
    ErrorCodes m_errorState;
};

and refer to it like follows:

void Date::errCode(Date::ErrorCodes errorCode) {
        m_errorState = errorCode;
}

Upvotes: 1

frslm
frslm

Reputation: 2978

Macros don't require the = operator since they're replaced by whatever you've defined for them. In your case, errorCode == NO_ERROR would equate to errorCode == = 0 instead of errorCode == 0 as you might've expected.

Just drop the equals signs:

#define NO_ERROR 0        // No error - the date is valid
#define CIN_FAILED 1      // istream failed on information entry
#define YEAR_ERROR 2      // Year value is invalid
#define MON_ERROR 3       // Month value is invalid
#define DAY_ERROR 4       // Day value is invalid

Upvotes: 3

Related Questions