1

Full source code of the file '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;
    }
}

Compiler Error:

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";
    ^------------------------------------------^

I don't see any duplicate declaration of ownerInfo. Any idea, why am I getting this error?

2

@Hari GTT Psicolabis' answer is right, but that means that you have to specify the ownerInfo field as public to get the benefit of a public getter.

string public ownerInfo = "Contract Owner name XYZ";

the alternative, is to change the name of your function.

  • 1
    You are right, i don't know why i though it was already public – Hari GTT Psicolabis Jun 4 '18 at 15:02
  • I forgot to change the method name to 'getOwnerName'! And happy to know additional information on 'automatically generated getters' in Solidity. @mirg, Changing the name alone works: string ownerInfo = "x"; function getOwnerInfo() public view returns (string) { return ownerInfo; }. @hari-gtt-psicolabis, Thanks! Just declaring variable as public also works: string public ownerInfo = "x"; function ownerInfo() public view returns (string) { return ownerInfo; }. I accepted @mirg's answer, since exposing attribute as 'public' isn't a desired solution. But, have upvoted :-) – RafiAlhamd Jun 4 '18 at 16:26
3

This is because the method and the property have the same name. Since solidity automatically creates a public getter function for every public variable in the contract, you don't need function ownerInfo() public view returns (string)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.