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


// 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.

上一篇:php 无损压缩图片


下一篇:C++基础笔记(二)C++对C的扩展