Solidity

TypeError:索引表達式必須是類型、映射或數組(是函式(地址)視圖外部返回(字元串記憶體、字元串記憶體…)

  • August 15, 2019

我有 2 份契約,第一份是次要的,第二份是主要的。首先,我部署 main 並創建一個新的子令牌。然後我想在 subToken 契約中訪問我的主契約,但我不能:(當我編譯這個契約時,我在 getName2 函式中看到了這個錯誤:

browser/opz4.sol:17:16: TypeError: Indexed expression has to be a type, mapping or array (is function (address) view external returns (string memory,string memory,uint8,uint256,uint256)) return mc.tokens$$ address(this) $$。姓名; ^——–^

pragma solidity ^0.5.0; 
import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/SafeMath.sol'; 

contract SubToken{ 
   MainContract mc;

   constructor(address _address) public{  
      mc=MainContract(_address);

   }  

   function getName1() view public returns(string memory){
       return mc.getNameMain(address(this));
   }

   function getName2() view public returns(string memory){
       return mc.tokens[address(this)].name;
   }

   //function transferFrom(address from, address to, uint tokens) public returns (bool success); 
   //function allowance(address tokenOwner, address spender) public view returns (uint remaining);  
   //function transfer(address to, uint tokens) public returns (bool success) ;  
   //function approve(address spender, uint tokens) public returns (bool success);  
   //function transferFrom(address from, address to, uint tokens) public returns (bool success);  

   event Transfer(address indexed from, address indexed to, uint tokens); 
   event Approval(address indexed tokenOwner, address indexed spender, uint tokens); 

} 
contract MainContract {
   //mapping(address => mapping(address=>uint)) balances;

   //mapping(address => mapping(address => uint)) allowed;

   address[] public contracts;

   using SafeMath for uint;

   address contract_address;

   struct token{ 
       string  name; 
       string   symbol; 
       uint8  decimals; 
       uint  totalSupply; 
       uint balances;
   } 
   mapping(address => token) public tokens;

   address mycontraddress;

   function createNewContract(string memory _name,string memory _symbol,uint8 _decimals,uint256 _totalSupply,uint _balance) public returns(address youraddress){ 
       SubToken st = new SubToken(address(this));//address(this)
       tokens[address(st)] = token(_name,_symbol,_decimals,_totalSupply,_balance);
       mycontraddress=address(st);
       return mycontraddress;
   } 
   function seeContracttAddress() public view returns(address){
       return mycontraddress;
   }

   function getNameMain(address _address) public view returns(string memory){
       return tokens[_address].name;
   }

   event Transfer(address indexed _from, address indexed _to, uint256 _value);
   event Approval(address indexed _owner, address indexed _spender, uint256 _value);

}

任何人都可以幫助我:)

我認為這種行為是由於您在契約之間傳遞結構。您可能知道它們應該在內部傳遞。那就是說你需要使用getNameMain

在這裡你會發現一個類似的討論 ,使用契約 C 將契約 A 中的結構複製到契約 B 中的結構中

引用自:https://ethereum.stackexchange.com/questions/73958