solidity 0.8.0版本现已允许函数直接返回结构体,所以尝试在其他合约中调用
被调合约:
// SPDX-License-Identifier: MIT
pragma solidity 0.8.0;
contract Deployed {
Nft[] public a;
struct Nft {
uint256 a;
uint256 b;
}
function setA(uint256 v) public {
a.push(Nft({a: v, b: v}));
}
function ge(uint256 _index) public view returns (Nft memory) {
return a[_index];
}
}
调用合约:
// SPDX-License-Identifier: MIT
pragma solidity 0.8.0;
//Deployed合约的接口
interface Deployed {
struct Nft {
uint256 a;
uint256 b;
}
function setA(uint256) external;
function a(uint256) external view returns (Nft memory);
function ge(uint256 _index) external view returns (Nft memory);
}
contract Existing {
Deployed dc;
struct Nft {
uint256 a;
uint256 b;
}
constructor(address _address) {
dc = Deployed(_address);
}
function getA(uint256 _index) public view returns (Nft memory) {
Nft memory c = dc.a(_index);
return c;
}
function setA(uint256 _v) public {
dc.setA(_v);
}
}
调用合约中getA方法报错:Return argument type struct Deployed.Nft memory is not implicitly convertible to expected type (type of first return variable) struct Existing.Nft memory.
如何才能在调用合约中拿到被调合约返回的结构体