Solidity

TypeError:在地址payable []儲存參考中進行參數相關查找後,成員“推送”未找到或不可見

  • December 29, 2021

在將新創建的合約地址推送到 contracts[] 數組時,我遇到了上述錯誤。我哪裡錯了?任何幫助將不勝感激。

pragma solidity ^0.8.0;

contract Bakery {

 // index of created contracts

 address[] public contracts;
 Cookie public cookieContract;

 function getContractCount() 
   public
   
   returns(uint contractCount)
 {
   // return contracts.length;
 }

 // deploy a new contract

 function newCookie(string memory _name)
   public
   returns(Cookie newContract)
 {
   newContract = new Cookie(_name);
   contracts.push(newContract);   ///**Line Having Error***//////
   return newContract;
 }

 function getCookieName (Cookie _cookieContract) public returns(string memory name) {
     cookieContract = _cookieContract;
     string memory n =  cookieContract.name();
     return n;
 }
}


contract Cookie {

   //name
   string public name;

    constructor (string memory _name) public{
       name = _name;
   }

 // suppose the deployed contract has a purpose

 function getFlavor()
   public
   pure
   returns (string memory flavor)
 {
   return "mmm ... chocolate chip";
 }    
}```

問題是contracts被聲明為

address[] public contracts;

所以它只會接受地址。

newContract您可以通過轉換為地址來修復它

function newCookie(string memory _name)
 public
 returns(Cookie newContract)
{
 newContract = new Cookie(_name);
 contracts.push(address(newContract));
 return newContract;
}

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