c++ - MSVC多行宏编译器错误

标签 c++ macros

我在 header 中定义了一个宏,该宏所基于的函数也在同一 header 中。

这是一个非常基本的示例,不是确切的代码,但希望它能足以说明问题:

myMacro.h:

    #ifndef MYMACRO_H
        #define MYMACRO_H

        #ifdef _DEBUG
            bool myAssertFn(int test, char const* desc, char const* file, int line) {
                if (test != 0) {
                //Test passes, no action required
                    return true;
                }
                std::string msg;

                if (desc != nullptr) {
                    msg += "\n Context: ";
                    msg += desc;
                }
                if (file != NULL) {
                    msg += "\n    File: ";
                    msg += file;
                }
                if (line > 0) {
                    msg += "\n    Line: ";
                    msg += std::to_string(line);
                }
                //Construct filename
                time_t tClock = time(0);
                char szTime[24];
                tm tmNow;
                //Get system time
                localtime_s(&tmNow, &tClock);
                //Assertion Log File, path and name
                static const char* assertLogFile = "./ALF.log";
                //Build time / date of day prefix
                sprintf_s(szTime, sizeof(szTime), "%04d/%02d/%02d %02d:%02d:%02d "
                        , tmNow.tm_year + 1900, tmNow.tm_mon + 1, tmNow.tm_mday
                        , tmNow.tm_hour, tmNow.tm_min, tmNow.tm_sec);
                //Does file exist?
                std::ofstream logFile(assertLogFile, std::ios_base::app);
                //Write the content to the file
                logFile << szTime << msg.c_str() << std::endl;
                return false;
            }
            //Macro
            #define myAssert(test, desc)\               
                        myAssertFn((test), (desc), __FILE__, __LINE__)      
        #else
            #define myAssert(test, desc)\
                        (void)0
        #endif
    #endif

该宏的目的是包括调试信息,并替代标准的assert函数,并具有将结果记录到文件中的附加好处。

问题是编译时得到:
error C2065: 'test' : undeclared identifier
error C2065: 'desc' : undeclared identifier

在源文件中使用宏的其他错误:
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2365: 'myAssertFn' : redefinition; previous definition was 'function' myAssert.h(37) : see declaration of 'myAssertFn'
error C2078: too many initializers
error C2143: syntax error : missing ';' before 'const'

在任何需要此宏的源文件中,我只需包含 header 并按如下所示使用宏:
    myAssert(ptr != NULL, "ptr != NULL");

如果测试返回0,则描述将记录到文件中,并带有日期,时间戳,文件名和发生错误的行号。

最佳答案

进行定义时,您将使用尾随字符:

#define myAssert(test, desc)\               
                              ^^^^^^^^^^^^^^

这意味着您的反斜杠适用于空格,而不适用于最终行,因此myAssertFn((test), (desc), __FILE__, __LINE__)的使用不再是宏的一部分。

您在源文件上的错误很可能是由于您没有从头文件中删除定义,因此存在重定义错误。

关于c++ - MSVC多行宏编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60409257/

相关文章:

c++ - 令人困惑的枚举初始化

c++ - Proactor 和异步写入

c++ - 如何使预处理器宏变得贪婪?

scala - 是否可以定义一个带有可变参数的宏,并为每个参数获取一个类型?

scala - 在宏中使用私有(private)构造函数

c++ - 在 boost::program_options 解析我的命令行参数后,如何获取非标志和非选项标记

c++ - 如何通过一些计算创建常量数组

c - 动态获取消息定义

macros - set-macro-character 的非终止-p 参数有什么作用?

c++ - 使用#pragma 和#ifdef 的多行宏