头文件中缺少 typedef 导致编译错误
missing typedef in header file causes compile error
我有一个头文件 monitor.hpp 包含:
#ifndef MONITOR_HPP_
#define MONITOR_HPP_
typedef unsigned short abc_status_id_t;
struct monitor_update
{
monitor_update(BYTE* data, size_t size) { /* implementation */ }
BYTE* data;
size_t dataSize;
};
class monitor_consumer
{
public:
virtual ~monitor_consumer() {};
virtual void updated(const monitor_update& update) = 0;
};
#endif // MONITOR_HPP_
请注意,上面没有 BYTE 的类型定义 - 长话短说 - 但其他使用的文件可能包含 Windows.h 或可能具有 BYTE 类型定义的内容。
但是我有这个 class 我需要#include那个头文件:
#ifndef BYTE
typedef unsigned char BYTE;
#endif
#include "monitor.hpp"
class mymonitor : public monitor_consumer
{
public:
void updated(const monitor_update& update) { }
};
int main() {
}
如果我注释掉 #ifndef BYTE,我会得到:
monitor.hpp(9): error C2061: syntax error : identifier 'BYTE'
monitor.hpp(10): error C2143: syntax error : missing ';' before '*'
monitor.hpp(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
定义修复有效,我在编译和链接时没有遇到任何问题。但这是最好的方法吗?我还有哪些其他选择?
您正在使用的某些 headers 似乎在做 #define BYTE <something>
,这是一件相当不友好的事情。
您可以尝试找出是否可以删除或禁用 [=12=](某些 Windows headers 允许您有选择地关闭其中的一部分)。
否则,您的解决方案是应对恶劣环境的合理方式。另一种选择是
#undef BYTE
typedef unsigned char BYTE;
我有一个头文件 monitor.hpp 包含:
#ifndef MONITOR_HPP_
#define MONITOR_HPP_
typedef unsigned short abc_status_id_t;
struct monitor_update
{
monitor_update(BYTE* data, size_t size) { /* implementation */ }
BYTE* data;
size_t dataSize;
};
class monitor_consumer
{
public:
virtual ~monitor_consumer() {};
virtual void updated(const monitor_update& update) = 0;
};
#endif // MONITOR_HPP_
请注意,上面没有 BYTE 的类型定义 - 长话短说 - 但其他使用的文件可能包含 Windows.h 或可能具有 BYTE 类型定义的内容。
但是我有这个 class 我需要#include那个头文件:
#ifndef BYTE
typedef unsigned char BYTE;
#endif
#include "monitor.hpp"
class mymonitor : public monitor_consumer
{
public:
void updated(const monitor_update& update) { }
};
int main() {
}
如果我注释掉 #ifndef BYTE,我会得到:
monitor.hpp(9): error C2061: syntax error : identifier 'BYTE'
monitor.hpp(10): error C2143: syntax error : missing ';' before '*'
monitor.hpp(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
定义修复有效,我在编译和链接时没有遇到任何问题。但这是最好的方法吗?我还有哪些其他选择?
您正在使用的某些 headers 似乎在做 #define BYTE <something>
,这是一件相当不友好的事情。
您可以尝试找出是否可以删除或禁用 [=12=](某些 Windows headers 允许您有选择地关闭其中的一部分)。
否则,您的解决方案是应对恶劣环境的合理方式。另一种选择是
#undef BYTE
typedef unsigned char BYTE;