0

Why is the line '''SecretVault _secretVault = SecretVault(secretVault);''' in the following code throwing compilation error "DeclarationError: Undeclared identifier....."

// SPDX-License-Identifier: MIT

pragma solidity >=0.7.0 <0.9.0;

/**
 * @title SixthContract
 * @dev Exploring inheritance, factories and interaction with other smart contracts.
 */

contract Ownable {

    address owner;

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


    modifier onlyOwner() {
        require(msg.sender == owner, "Must be Owner.");
        _;
    }
}

contract SecretVault {
    string secret;

    constructor(string memory _secret) public {
        secret = _secret;
    }

    function getSecret() public view returns(string memory) {
        return secret;
    }
}

contract SixthContract is Ownable {

    string secret;

    constructor (string memory _secret) public {
        SecretVault _secretVault = new SecretVault(_secret);
        secret = _secret;
        super;
    }

    function getSecret() public view onlyOwner returns(string memory) {
        SecretVault _secretVault = SecretVault(secretVault);
        return _secretVault.getSecret();
    }
    
}
1
  • Probably, it should be written there: SecretVault _secretVault = SecretVault(secret); but there will be further conversion errors.
    – Mad Jackal
    Commented Nov 28, 2021 at 11:52

1 Answer 1

0

There are a couple of mistakes in SixthContract contract

  1. Usage of super in the constructor doesn't make much sense

     super;
    
  2. getSecret uses a variable secretVault that's not defined anywhere.

     SecretVault _secretVault = SecretVault(secretVault);
    

    I'd guess you want that variable at the contract level and initialize it in the constructor so it can be used from the view function.

contract SixthContract is Ownable {

    string secret;
    SecretVault secretVault;

    constructor (string memory _secret) public {
        secretVault = new SecretVault(_secret);
        secret = _secret;
    }

    function getSecret() public view onlyOwner returns(string memory) {
        return secretVault.getSecret();
    }
    
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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