【发布时间】:2015-12-11 21:24:58
【问题描述】:
考虑以下代码:
std::ostream file;
if( file == NULL ) std::cout << "Failed to open file" << std::endl;
在传递 -std=gnu11(GCC 5.2 的默认值)或仅使用gcc code.cpp -o a.out 时,它可以完美编译。
-std=gnu++11 失败,但是:
no match for ‘operator==’ (operand types are ‘std::ofstream {aka std::basic_ofstream<char>}’ and ‘long int’)`
最简单的解决方法是什么?
详情:
我必须使用 std=gnu++11 才能访问shared_ptr 定义。此外,我的一些代码是自动生成的,修改生成器需要付出合理的努力——所以我想知道是否有人能想出更简单的方法来摆脱这种“缺乏兼容性”。
【问题讨论】:
-
if (!file) std::cout << "Failed."; -
-std=gnu11是一个 c 标志 -
if( file == nullptr ) -
@SergeRogatch 查看我的回答;这在 C++11 中发生了变化。
-
@SergeRogatch 即使合法,将对象与指针进行比较有什么意义?投射到
bool有点不同,也更有意义。