首页 > 编程语言 > 详细

c/c++ 重载 数组 操作符[] operator[ is ambiguous, as 0 also mean a null pointer of const char* type.

时间:2014-03-04 00:28:28      阅读:487      评论:0      收藏:0      [点我收藏+]

// Note: //int x = a[0].GetInt(); // Error: operator[ is ambiguous, as 0 also mean a null pointer of const char* type. int y = a[SizeType(0)].GetInt(); // Cast to SizeType will work. int z = a[0u].GetInt(); // This works too.

0u  = SizeType(0)
 

Json::Value作为数组时,读取0位置时,出现错误:Use of overloaded operator [] is ambiguous

Use of overloaded operator [] is ambiguous (with operand types ‘const Json:Value‘ and ‘int‘ )

 

    void Parse(constJson::Value &jsonObject) {

      rmb = jsonObject[0].asDouble();

    }

 

Json:Value[]运算符支持输入的非负整型,即UInt或unsigned int。

而0作为索引值,也可能为空指针输入, 类型检查无法通过。

 

修改如下即可:

rmb = jsonObject[0U].asDouble();

或者:

rmb = jsonObject[SizeType(0)].asDouble();

 

说明:  0U   表示 无符号的整数,   这样写在自己的数组操作符重载的时候 区分 0 是 数值0 还是 空指针了....

 

c/c++ 重载 数组 操作符[] operator[ is ambiguous, as 0 also mean a null pointer of const char* type.,布布扣,bubuko.com

c/c++ 重载 数组 操作符[] operator[ is ambiguous, as 0 also mean a null pointer of const char* type.

原文:http://www.cnblogs.com/porter/p/3578264.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!