简体   繁体   English

使用 new 关键字在数组中添加结构类似于在另一个合同中添加合同

[英]Adding structure in array using new keyword similar to adding contract in another contract

Below is the source code which I wrote to deploy one smart contract using another smart contract下面是我编写的使用另一个智能合约部署一个智能合约的源代码

//SPDX-License-Identifier: MIT
pragma solidity 0.8.17;

import "./contractone.sol" ;

contract contracttwo
{
      contractone[] public deploy ;

    function contwo() public
    {  deploy.push(new contractone());  // able to add instance of data type i.e contractone using 'new'
    }
}

Now this contract worked.现在这份合同生效了。 Similar to this I wrote another contract与此类似我写了另一个合同

//SPDX-License-Identifier: MIT
pragma solidity 0.8.16 ;
 contract arr
 { people[] public person ;
    struct people
     {
         uint256 num ;
         string name ;
     }

      function plus() public
     {   
       person.push(new people()); // unable to new instance of data type i.e people using new       
     }
 }

In this contract when I tried to add new instance of people, I got the error在此合同中,当我尝试添加新的人员实例时,出现错误

Error upon trying 'new' keyword尝试“新”关键字时出错

It read that:-上面写着:-

TypeError: Identifier is not a contract.类型错误:标识符不是合同。

I know first is case of contract and second is of structure.But the thing which I am trying to do is same ie trying to new instance of data of the array.Can anyone please tell me why new keyword new is not working in second contract?我知道第一个是合同的情况,第二个是结构的情况。但我想做的事情是一样的,即尝试数组的新数据实例。谁能告诉我为什么 new 关键字new在第二个合同中不起作用?

You dont need to use "new" for second case.第二种情况不需要使用“new”。

function plus(uint _num, string memory _name) public {  
person.push(people(_num, _name));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM