如何将结构作为参数从一个合同传递到另一个合同

How to pass struct as argument from one contract to another contract

我可以将数组、uint、bool 映射从一个合约传递到另一个合约。但我不能将结构作为参数传递,

//SPDX-License-Identifier: UNLICENSED

pragma solidity 0.8.11;

interface SampleInterface {
        struct Point {
                uint256 x;
                uint256 y;
        }
        function setPoint(Point memory) external;
        function getPoint() external view returns(Point memory);
}

contract Sample is SampleInterface {
    Point point;
    function setPoint(Point memory _point) external {
        point = _point;
    }

    function getPoint() external view returns(Point memory) {
        return point;
    }
}

这是我的源合同。我已经部署了上面的合约,并将合约地址放到下面的合约中调用setPoint和getPoint,

//SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.11;

interface SampleInterface {
    struct Point {
        uint256 x;
        uint256 y;
    }
    function setPoint(Point memory point) external;
    function getPoint() external view returns(Point memory);
}
contract Operator {
    address sample;
    constructor(address _sample) {
        sample = _sample;
    }

    function setPoint(Point memory point) external {
        SampleInterface(sample).setPoint(point);
    }

    function getPoint() external view returns(Point memory) {
        return SampleInterface(sample).getPoint();
    }
}

但是我收到以下错误,

Compiling your contracts...
===========================
> Compiling ./contracts/Operator.sol

DeclarationError: Identifier not found or not unique.
  --> project:/contracts/Operator.sol:18:20:
   |
18 |    function setPoint(Point memory point) external {
   |                      ^^^^^

Compilation failed. See above.
Truffle v5.5.9 (core: 5.5.9)
Node v12.14.0

帮我解决这个问题。如何从 Operator 合约中调用 setPoint 和 getPoint。

Point 结构不存在于您的合同范围内,仅存在于您声明的接口中。要解决此问题,您需要使用 SampleInterface.Point 而不是 Point

function setPoint(SampleInterface.Point memory point) external {
    SampleInterface(sample).setPoint(point);
}

function getPoint() external view returns(SampleInterface.Point memory) {
    return SampleInterface(sample).getPoint();
}