gpt4 book ai didi

c++ - 宏需要一个表达式

转载 作者:行者123 更新时间:2023-11-30 05:07:19 27 4
gpt4 key购买 nike

我目前正在做一个学校项目,我正在做的部分作业的要求如下所示:

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"

和:

void errCode(int errorCode);

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

但是,当我将代码放入第二个函数时,当我尝试在 if 语句中使用宏值时,visual studio 给我一条红色下划线并指出“需要一个表达式”。

// 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)
{
}
}

头文件定义如下:

#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

最佳答案

宏不需要 = 运算符,因为它们会被您为它们定义的任何内容替换。在您的情况下,errorCode == NO_ERROR 将等同于 errorCode == = 0 而不是您可能期望的 errorCode == 0

只需去掉等号:

#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

关于c++ - 宏需要一个表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47519168/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com