简体   繁体   English

TypeError:返回参数类型元组(int_const 23,bool,uint8 [3]内存)不可转换为预期类型元组(uint256,bool,uint256 [3]内存)

[英]TypeError: Return argument type tuple(int_const 23,bool,uint8[3] memory) is not convertible to expected type tuple(uint256,bool,uint256[3] memory)

//When I compile and deploy below code. //当我编译和部署下面的代码时。 It gives me no error它没有给我任何错误

// SPDX-License-Identifier: MIT 
pragma solidity ^0.8.0; 
contract MultipleValues{
function returnValues() public pure returns (uint, bool, uint8[3] memory) {
    return (23, true, [1,2,3]);
}
}

//But when I change value from uint8[3] to uint[3] then it throws error //但是当我将值从 uint8[3] 更改为 uint[3] 时,它会抛出错误

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract MultipleValues{function returnValues() public pure returns (uint, bool, uint[3] memory) {
return (23, true, [1,2,3]);
}
}
//how to resolve this issue?
//Unexpected behaviour of array in solidity when returning a fixed size array

this should work:这应该有效:

contract MultipleValues{
    function returnValues() public pure returns (uint, bool, uint[3] memory) {
        uint[3] memory memoryArray;
        memoryArray[0]=1;
        memoryArray[1]=2;
        memoryArray[2]=3;
        return (23, true, memoryArray);
}

uint types in solidity are uint8,uint16,uint24,.....,uint256 . solidity 中的uint类型是uint8,uint16,uint24,.....,uint256 The smallest one is uint8 and when the compiler sees "[1,2,3]" it infers the smallest uint type which is uint8 .最小的是uint8 ,当编译器看到“[1,2,3]”时,它会推断出最小的 uint 类型,即uint8 Actually "[1,2,3]" can be any of uint8,uint16,uint24,.....,uint256实际上“[1,2,3]”可以是uint8,uint16,uint24,.....,uint256的任意一个

What I did was, explicitly told the compiler that I am creating "uint[3]" and returning it.我所做的是,明确告诉编译器我正在创建“uint[3]”并返回它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM