Reputation: 582
I am new to Solidity. I am using an interface and a library for an exercise in Solidity. The contract must implement the methods from the interface, with the help of the functions from the library. I get a Declaration Error: Undeclared identifier for mapPerson and mapCompany. Where do I go wrong? I have tried to put the structs in the library, but then I get other errors, because of the logic I have implemented. Here is my code:
//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);
}
Upvotes: 3
Views: 1965
Reputation: 1387
In your smart contract code you must to define the mappings inside the 'contract' keyword. In your case remove the mapPerson
and map Company
from the struct mappings and put them after the keyword 'contract' in this way:
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;
...
UPDATED ANSWER For use a mapping into a library, you must to use a struct in which we can declare the mapping. At this point in your smart contract, you must define the instance of structs created in the library and this state variables must to be passed when you want to read/insert/update items in the mapping. Said that, try this: Said that, try this solution:
//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);
}
Upvotes: 1
Reputation: 91
These lines should be outside of the structs (Person
, Company
):
mapping(address => Person) mapPerson;
mapping(address => Company) mapCompany;
like :
struct Person{
address addr;
string name;
string surname;
}
mapping(address => Person) mapPerson;
Upvotes: 1