Home > Software engineering >  Trying to push into a struct array
Trying to push into a struct array

Time:06-24

I am getting this error:

contracts/Simple Contracts/NFT.sol:21:18: ParserError: Expected identifier but got '(' nftArray.push(nftOne); ^

// Struct for storing our NFT's
struct NFT {
    string contractAddress;
    uint256 tokenID;
    string tokenStandard;
    string blockChain;
    uint256 creatorFees;
}

NFT nftOne = NFT("0xD9D24a4b8A58BCdBfDDd0d27B51F27", 345,"ERC-721", "ETHEREUM", 721);
NFT nftTwo = NFT("0xD9824a4b8A5u8CdBfDDd0d27B51F27", 346,"ERC-721", "ETHEREUM", 721);
NFT nftThree = NFT("0xD9DE9824a4b8A58BCdBfDDd0Y1F27", 347,"ERC-721", "ETHEREUM", 721);

NFT[] nftArray;
nftArray.push(nftOne);

CodePudding user response:

The reason you get this error is because the solidity compiler expects variable or function identifier, but got an expression nftArray.push(nftOne);. Consider moving it to a constructor like this:

struct NFT {
    string contractAddress;
    uint256 tokenID;
    string tokenStandard;
    string blockChain;
    uint256 creatorFees;
}

NFT nftOne = NFT("0xD9D24a4b8A58BCdBfDDd0d27B51F27", 345,"ERC-721", "ETHEREUM", 721);
NFT nftTwo = NFT("0xD9824a4b8A5u8CdBfDDd0d27B51F27", 346,"ERC-721", "ETHEREUM", 721);
NFT nftThree = NFT("0xD9DE9824a4b8A58BCdBfDDd0Y1F27", 347,"ERC-721", "ETHEREUM", 721);

NFT[] nftArray;

constructor() {
    nftArray.push(nftOne);
}
  • Related