Solidity

Solidity 編譯器 - 錯誤:標識符已聲明

  • July 5, 2021

文件“Test.sol”的完整原始碼:

pragma solidity ^0.4.23;


contract Mortal{

   address owner;

   string ownerInfo = "Contract Owner name XYZ";

   constructor () public {
       owner = msg.sender;
   }

   function kill () public {
       if (msg.sender == owner) {
           selfdestruct(owner);
       }
   }

   function ownerInfo() public view returns (string) {
       return ownerInfo;
   }

}


contract Test is Mortal {

   string message = "Hello World!";

   constructor () public {
   }

   function getMessage() public view returns (string){
       return message;
   }
}

編譯器錯誤:

myMac:solidity admin$ solc Test.sol
Test.sol:21:2: Error: Identifier already declared.
   function ownerInfo() public view returns (string) {
^ (Relevant source part starts here and spans across multiple lines).
Test.sol:9:2: The previous declaration is here:
   string ownerInfo = "Contract Owner name XYZ";
   ^------------------------------------------^

我沒有看到任何重複ownerInfo. 任何想法,為什麼我會收到這個錯誤?

@Hari GTT Psicolabis 的回答是正確的,但這意味著您必須指定該ownerInfo欄位public以獲得公共吸氣劑的好處。

string public ownerInfo = "Contract Owner name XYZ";

另一種方法是更改​​函式的名稱。

這是因為方法和屬性具有相同的名稱。由於solidity會自動為合約中的每個公共變數創建一個公共getter函式,所以你不需要function ownerInfo() public view returns (string)

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