问答

__cplusplus使用问题

作者:admin 2021-05-11 我要评论

某客时间大佬专栏,大佬写的代码 #ifdef __cplusplus // 定义了这个宏就是在用C++编译 extern "C" { // 函数按照C的方式去处理#endif void a_c_function(int a);...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

某客时间大佬专栏,大佬写的代码

#ifdef __cplusplus                      // 定义了这个宏就是在用C++编译
    extern "C" {                        // 函数按照C的方式去处理
#endif
    void a_c_function(int a);
#ifdef __cplusplus                      // 检查是否是C++编译
    }                                   // extern "C" 结束
#endif

#if __cplusplus >= 201402                // 检查C++标准的版本号
    cout << "c++14 or later" << endl;    // 201402就是C++14
#elif __cplusplus >= 201103              // 检查C++标准的版本号
    cout << "c++11 or before" << endl;   // 201103是C++11
#else   // __cplusplus < 201103          // 199711是C++98
#   error "c++ is too old"               // 太低则预处理报错
#endif  // __cplusplus >= 201402         // 预处理语句结束

为何我编写demo老是报错呢?请各位大佬指出问题,出错最小demo附上:

#include <iostream>
using namespace std;

#if __cplusplus >= 201402 // 检查C++标准的版本号
    cout << "c++14 or later" << endl;    // 201402就是C++14
#elif __cplusplus >= 201103              // 检查C++标准的版本号
    cout << "c++11 or before" << endl;   // 201103是C++11
#else   // __cplusplus < 201103          // 199711是C++98
#   error "c++ is too old"               // 太低则预处理报错
#endif  // __cplusplus >= 201402         // 预处理束

int main()
{
    return 0;
}

[root c++]#g++ cplusplus.cc
cplusplus.cc:5:5: error: ‘cout’ does not name a type

 cout << "c++14 or later" << endl;    // 201402就是C++14
 ^~~~

包含了using namespace std,cout这里居然还是报错,及时修改成std::cout也报同样的错误,是我哪里没搞对,还是本来就不能这么用??预处理这里能这么用吗?

###
cout << "c++14 or later" << endl;

你要写到函数体内,cpp 又不是脚本解释的。
你直接这样写全局里,不报错才怪。

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • __cplusplus使用问题

    __cplusplus使用问题

  • VPN输入框我并没有开启大写模式,总是

    VPN输入框我并没有开启大写模式,总是

  • Bizcharts使用value属性报错

    Bizcharts使用value属性报错

  • webstorm新建Node.js Express App识别

    webstorm新建Node.js Express App识别

腾讯云代理商
海外云服务器