Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 编译器的行为就像预处理器指令是';t定义_C++ - Fatal编程技术网

C++ 编译器的行为就像预处理器指令是';t定义

C++ 编译器的行为就像预处理器指令是';t定义,c++,C++,我有一个头文件和它的cpp文件(Error.h,Error.cpp)。cpp文件对预处理器指令执行检查,但总是失败 错误。h: /* Optional macros: AE_EXIT_AT_ERROR AE_CONSOLE_WRITE_AT_ERROR */ #pragma once extern void aeError(const char *str, int code=1); extern void aeAssert(bool b, const char *failStr = "ass

我有一个头文件和它的cpp文件(Error.h,Error.cpp)。cpp文件对预处理器指令执行检查,但总是失败

错误。h:

/*
Optional macros:
AE_EXIT_AT_ERROR
AE_CONSOLE_WRITE_AT_ERROR
*/
#pragma once

extern void aeError(const char *str, int code=1);

extern void aeAssert(bool b, const char *failStr = "assertion failed");
Error.cpp:

#include "Error.h"
#include <stdexcept>
#ifdef AE_CONSOLE_WRITE_AT_ERROR
#include <iostream>
#endif

void aeError(const char *str, int code)
{
    #ifdef AE_CONSOLE_WRITE_AT_ERROR
    std::cout << str << std::endl;
    #endif

    throw std::runtime_error(str);

    #ifdef AE_EXIT_AT_ERROR
    std::exit(code);
    #endif
}

void aeAssert(bool b, const char *failStr)
{
    if(!b)
        aeError(failStr);
}

std::cout
main.cpp
Error.cpp
都是不同的翻译单位。只能为
main.cpp
定义宏,而不能为
Error.cpp
定义宏


您应该将
#define
指令放在两个.cpp文件包含的头文件中,或者在项目设置/makefile中定义这些宏。

抛出后的退出将永远不会执行。为什么?它应该在运行时错误之后执行。(我想我可以在弹出的消息窗口中选择“继续”)在
throw
语句之后,函数退出。其他任何行为都是非标准行为。
//define both macros:
#define AE_CONSOLE_WRITE_AT_ERROR
#define AE_EXIT_AT_ERROR
#include "Error.h"

//rest of code
//...