如何解决在Windows程序中使用了C++标准库文件后
答案:1 悬赏:0
解决时间 2021-11-10 07:12
- 提问者网友:冰点阿弟
- 2021-11-09 09:14
如何解决在Windows程序中使用了C++标准库文件后
最佳答案
- 二级知识专家网友:冷眼_看世界
- 2021-11-09 10:44
在Windows 的程序设计中,使用了标准库 ( STL ) 后就产生了很多语法错误,可想而知它们两者之间有冲突。原因就在于一方面 min 和 max 宏在 Windef.h 定义成:
#ifndef NOMINMAX
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))#endif#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))#endif#endif
这里的 Windef.h 是被 Windows.h 所包含的,而 Windows.h 几乎被所有的基于 Windows 的程序所包含。另一方面,min 和 max 也在 STL 中的 Algobase.h 文件中被定义了:
template <class T>
inline const T& min(const T& a, const T& b) {
return b < a ? b : a;}template <class T, class Compare>
inline const T& min(const T& a, const T& b, Compare comp) {
return comp(b, a) ? b : a;}template <class T>
inline const T& max(const T& a, const T& b) {
return a < b ? b : a;}template <class T, class Compare>
inline const T& max(const T& a, const T& b, Compare comp) {
return comp(a, b) ? b : a;}已经在 Windef.h 中定义成了一个宏,同样名称的函数又在 STL 中被定义了,由此可知,当你同时包含了 Windef.h( 或者 Windows.h ) 和 Algobase.h ( 来自 STL ) 时,就会产生语法错误。所以,我们要屏蔽去掉其中的一个定义。
---------------------------------------------------------------
下面的做法是用 NOMINMAX 来屏蔽掉 Windef.h 中的 min 和 max .
#ifndef NOMINMAX
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))#endif#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))#endif#endif
这里的 Windef.h 是被 Windows.h 所包含的,而 Windows.h 几乎被所有的基于 Windows 的程序所包含。另一方面,min 和 max 也在 STL 中的 Algobase.h 文件中被定义了:
template <class T>
inline const T& min(const T& a, const T& b) {
return b < a ? b : a;}template <class T, class Compare>
inline const T& min(const T& a, const T& b, Compare comp) {
return comp(b, a) ? b : a;}template <class T>
inline const T& max(const T& a, const T& b) {
return a < b ? b : a;}template <class T, class Compare>
inline const T& max(const T& a, const T& b, Compare comp) {
return comp(a, b) ? b : a;}已经在 Windef.h 中定义成了一个宏,同样名称的函数又在 STL 中被定义了,由此可知,当你同时包含了 Windef.h( 或者 Windows.h ) 和 Algobase.h ( 来自 STL ) 时,就会产生语法错误。所以,我们要屏蔽去掉其中的一个定义。
---------------------------------------------------------------
下面的做法是用 NOMINMAX 来屏蔽掉 Windef.h 中的 min 和 max .
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯