Solidity

如何解決此 Remix 編譯錯誤“TypeError: Invalid array length, expected integer literal or constant expression & Data location”

  • April 23, 2022

以下是我在使用 balanceOf 函式的程式碼時遇到的錯誤:

TypeError:函式中參數的數據位置必須是“memory”或“calldata”,但沒有給出。–> 合約/Htoken.sol:39:24: | 39 | 函式 balanceOf(地址

$$ msg.sender $$) 公共覆蓋視圖返回 (uint256) { | ^^^^^^^^^^^^^^^^^^^^ TypeError:無效的數組長度,預期的整數文字或常量表達式。–> 合約/Htoken.sol:39:33: | 39 | 函式 balanceOf(地址

$$ msg.sender $$) 公共覆蓋視圖返回 (uint256) { | ^^^^^^^^^^ 契約守則

合約 NewToken {

string public constant name = "NewToken";
string public constant symbol = "NTKN";
uint8 public constant decimals = 18;
uint256 totalSupply_ = 10000000000000000000000000; 

mapping(address => uint256) public balances;
mapping(address => mapping (address => uint256)) allowed;

event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);

建構子(){餘額

$$ msg.sender $$= totalSupply_; }

function totalSupply() public override view returns (uint256) {
return totalSupply_;
}

function balanceOf(address [msg.sender]) public override view returns (uint256) {
   return balances[msg.sender];
}

function transfer(address to, uint256 value) public override returns (bool) {
   require(value <= balances[msg.sender]);
   balances[msg.sender] = balances[msg.sender]-value;
   balances[to] = balances[to]+value;
   emit transfer(msg.sender, to, value);
   return true;
}

function approve(address spender, uint256 value) public override returns (bool) {
   allowed[msg.sender][spender] = value;
   emit Approval(msg.sender, spender, value);
   return true;
}

function allowance(address owner, address spender) public override view returns (uint) {
   return allowed[owner][spender];
}

function transferFrom(address from, address to, uint256 value) public override returns (bool) {
   require(value <= balances[from]);
   require(value <= allowed[from][msg.sender]);

   balances[from] = balances[from]-value;
   allowed[from][msg.sender] = allowed[from][msg.sender]+value;
   balances[to] = balances[to]+value;
   emit Transfer(from, to, value);
   return true;
}

}

我用這個智能合約改變了你的智能合約實現。

我做筆記只是為了讓你知道你的錯誤。

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

// NOTE: Import IERC20 contract
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

// NOTE: You must to implement IERC20 for override ERC20 methods!
contract NewToken is IERC20 {

 string public constant name = "NewToken";
 string public constant symbol = "NTKN";
 uint8 public constant decimals = 18;
 uint256 totalSupply_ = 10000000000000000000000000; 

 mapping(address => uint256) public balances;
 mapping(address => mapping (address => uint256)) allowed;

 // NOTE: I removed the events because it already defined into IERC20
 
 constructor() { 
   balances[msg.sender] = totalSupply_;
 }

 function totalSupply() public override view returns (uint256) {
   return totalSupply_;
 }

 // NOTE: You must to change signature function adding address parameter
 function balanceOf(address _address) public override view returns(uint256) {
   return balances[_address];
 }

 function transfer(address to, uint256 value) public override returns (bool) {
     require(value <= balances[msg.sender]);
     balances[msg.sender] = balances[msg.sender]-value;
     balances[to] = balances[to]+value;
     // NOTE: The event is called  Transfer not 'transfer'
     emit Transfer(msg.sender, to, value);
     return true;
 }

 function approve(address spender, uint256 value) public override returns (bool) {
     allowed[msg.sender][spender] = value;
     emit Approval(msg.sender, spender, value);
     return true;
 }

 function allowance(address owner, address spender) public override view returns (uint) {
     return allowed[owner][spender];
 }

 function transferFrom(address from, address to, uint256 value) public override returns (bool) {
     require(value <= balances[from]);
     require(value <= allowed[from][msg.sender]);

     balances[from] = balances[from]-value;
     allowed[from][msg.sender] = allowed[from][msg.sender]+value;
     balances[to] = balances[to]+value;
     emit Transfer(from, to, value);
     return true;
 }
}

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