Declaration Error: Undeclared identifier Solidity

Declaration Error: Undeclared identifier Solidity

我是 Solidity 的新手。我正在使用一个界面和一个库来进行 Solidity 练习。合约必须借助库中的函数来实现接口中的方法。我收到声明错误:mapPerson 和 mapCompany 的未声明标识符。我哪里出错了?我曾尝试将结构放入库中,但由于我已实现的逻辑,随后出现其他错误。这是我的代码:

//SPDX-License-Identifier: UNLICENSED

pragma solidity ^0.7.0;

struct Person{
    address addr;
    string name;
    string surname;
}

struct Company{
    address addr;
    string name;
}


interface Interface{

    function addPerson(address addr, string memory name, string memory surname) external;
    
    function addCompany(address addr, string memory name) external;

    function getPerson() external view returns (address addr);

    function getCompany() external view returns (address addr);
}
    

library Lib{


    function addPerson(address addr, string memory name, string memory surname) public{
        addr = msg.sender;
        mapPerson[addr] = Person(name, surname, addr);
    }

    function addCompany(address addr, string memory name) public{
        addr = msg.sender;
        mapCompany[addr] = Company(addr, name);
    }     
}

contract Lab03 is Interface{

    Person p;
    Company c;

    mapping(address => Person) mapPerson;
    mapping(address => Company) mapCompany;

    function addPerson(address addr, string memory name, string memory surname) public override {
        Lib.addPerson(addr, name, surname);
        emit addPersonEvent(addr, name, surname);
    }

    function addCompany(address addr, string memory name) public override{
        Lib.addCompany(addr, name);
        emit addCompanyEvent(addr, name);
    }

    function getPerson() public override view returns (address addr) {
        return p.addr;
    }

    function getCompany() public override view returns (address addr) {
        return c.addr;
    }

    event addPersonEvent(address addr, string name, string surname);
    event addCompanyEvent(address addr, string name);
}

这些行应该在结构之外 (Person, Company):

mapping(address => Person) mapPerson;

mapping(address => Company) mapCompany;

喜欢:

struct Person{
    address addr;
    string name;
    string surname;
}
mapping(address => Person) mapPerson;

在您的智能合约代码中,您必须在 'contract' 关键字内定义映射。在您的情况下,从结构映射中删除 mapPersonmap Company 并以这种方式将它们放在关键字 'contract' 之后:

contract Lab03 is Interface{
    // Add here the mappings and remove them from the struct
    mapping(address => Person) mapPerson;
    mapping(address => Company) mapCompany;

    Person p;
    Company c;
 ...

更新的答案 要使用到库的映射,您必须使用我们可以在其中声明映射的结构。此时,在您的智能合约中,您必须定义在库中创建的结构实例,并且当您想要 read/insert/update 映射中的项目时,必须传递此状态变量。说,试试这个: 说,试试这个解决方案:

//SPDX-License-Identifier: UNLICENSED

pragma solidity ^0.7.0;

struct Person{
    address addr;
    string name;
    string surname;
}

struct Company{
    address addr;
    string name;
}


interface Interface{

    function addPerson(address addr, string memory name, string memory surname) external;
    
    function addCompany(address addr, string memory name) external;

    function getPerson(address addrFind) external view returns (address addr);

    function getCompany(address addrFind) external view returns (address addr);
}
    

library Lib{
    // define struct with mapping
    struct LibPerson {
        mapping(address => Person) mapPerson;
    }

    struct LibCompany {
        mapping(address => Company) mapCompany;
    }

    function addPerson(LibPerson storage lp, address addr, string memory name, string memory surname) public{
        addr = msg.sender;
        lp.mapPerson[addr] = Person(addr, name, surname);
    }

    function addCompany(LibCompany storage lc, address addr, string memory name) public{
        addr = msg.sender;
        lc.mapCompany[addr] = Company(addr, name);
    }    

    function getCompany(LibCompany storage lc, address addrFind) view external returns(address){
        return lc.mapCompany[addrFind].addr;
    }   

    function getPerson(LibPerson storage lp, address addrFind) view external returns(address){
        return lp.mapPerson[addrFind].addr;
    }     
}

contract Lab03 is Interface{
    // define state variable for two struct present in the library
    Lib.LibPerson libP;
    Lib.LibCompany libC;
    Person p;
    Company c;

    function addPerson(address addr, string memory name, string memory surname) public override {
        Lib.addPerson(libP, addr, name, surname);
        emit addPersonEvent(addr, name, surname);
    }

    function addCompany(address addr, string memory name) public override{
        Lib.addCompany(libC, addr, name);
        emit addCompanyEvent(addr, name);
    }

    function getPerson(address _addressFind) public override view returns (address addr) {
        return Lib.getPerson(libP, _addressFind);
    }

    function getCompany(address _addressFind) public override view returns (address addr) {
        return Lib.getCompany(libC, _addressFind);
    }

    event addPersonEvent(address addr, string name, string surname);
    event addCompanyEvent(address addr, string name);
}