【问题标题】:const char[] default function argumentconst char[] 默认函数参数
【发布时间】:2018-05-11 07:48:30
【问题描述】:

我已经定义了一个这样的类型:

typedef char sType[256];

还有一个函数,带有默认参数:

void foo(const sType param = NULL);

MinGW (g++ 4.8.0) 编译它没有错误。

相反,Visual Studio 2015 (Tools 14.0) 给出以下错误:

error C2040: 'sType': 'int' differs in levels of indirection from 'char [256]'

我尝试将NULL 转换为const char[],但这会导致:

error C2440: 'type cast': cannot convert from 'int' to 'const char []'

有什么提示吗?谢谢

【问题讨论】:

  • 尝试使用nullptr,而不是NULL(另外,将旧的损坏编译器更新为现代编译器mingw-w64.org/doku.php)。
  • 我收到error C2065: 'nullptr': undeclared identifier。尝试了thisnullptr 仿真),但我得到了其他错误。
  • 您使用的是古老的 c++11 之前的编译器,所以这不足为奇。
  • VS2015 应该足够新才能知道 c++11 和 nullptr.....
  • 似乎是一个 MSVC 错误。如果您改用(char *)NULL,它是否有效。 (顺便说一句,如果你不知道,const sType 在这种情况下是指char * const 而不是const char *

标签: c++ visual-studio visual-studio-2015 compiler-errors


【解决方案1】:

NULL 转换为const char[](或sType),而不是const char*。函数参数在very declaration 中经历数组到指针的衰减。

【讨论】:

  • 尝试强制转换为char *const char *,但错误仍然存​​在(“'int' 在间接级别上不同......”)。但是,它与我的项目特定的东西有关,因为我尝试从头开始一个简单的项目并且它有效。我想知道我是否应该取消这个线程...
猜你喜欢
  • 2016-05-23
  • 1970-01-01
  • 1970-01-01
  • 2013-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-23
  • 2019-01-31
相关资源
最近更新 更多