0

I am a novice to Solidity so you will have to bear with me regarding the following question:

I am trying to compile my first smart contract and keep on running into this Declaration issue. How do I correctly declare an address variable in Solidity?

Find below my simple code:

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

/**
 * @title Owner
 * @dev Set & change owner
 */
contract Gifter {

    address private gifter;
    //address private giftRecipient;
    
    // event for EVM logging
    event OwnerSet(address indexed gifter, address indexed giftRecipient);
    
    // modifier to check if caller is owner
    modifier isGifter() {
        require(msg.sender == gifter, "Gift NOT yours to give away!");
        _;
    }
    
    /**
     * @dev Set contract deployer as owner
     */
    constructor() {
        gifter = msg.sender; // 'msg.sender' is sender of current call, contract deployer for a constructor
        emit OwnerSet(address(0), gifter);
    }

    /**
     * @dev Change owner
     */
    function giftTo( address giftRecipient ) public isGifter {
        emit OwnerSet(gifter, giftRecipient);
        //gifter = giftRecipient;
        giftRecipient = giftRecipient;
    }

    /**
     * @dev Return owner address 
     * @return address of owner
     */
    function whoWasGifted() external view returns (address) {
        return giftRecipient;
    }
}

I keep getting the following error message:

DeclarationError: Undeclared identifier.
--> contracts/gifter.sol:45:16
|
45 | return giftRecipient;
| ^

1 Answer 1

2

Your giftRecipient declaration is commented out.

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

/**
 * @title Owner
 * @dev Set & change owner
 */
contract Gifter {

    address private gifter;
    address private giftRecipient;
    
    // event for EVM logging
    event OwnerSet(address indexed gifter, address indexed giftRecipient);
    
    // modifier to check if caller is owner
    modifier isGifter() {
        require(msg.sender == gifter, "Gift NOT yours to give away!");
        _;
    }
    
    /**
     * @dev Set contract deployer as owner
     */
    constructor() {
        gifter = msg.sender; // 'msg.sender' is sender of current call, contract deployer for a constructor
        emit OwnerSet(address(0), gifter);
    }

    /**
     * @dev Change owner
     */
    function giftTo( address _giftRecipient ) public isGifter {
        emit OwnerSet(gifter, _giftRecipient);
        //gifter = giftRecipient;
        giftRecipient = _giftRecipient;
    }

    /**
     * @dev Return owner address 
     * @return address of owner
     */
    function whoWasGifted() external view returns (address) {
        return giftRecipient;
    }
}

Noticed how I change the name for the function parameter to be _giftRecipient to avoid problems with the declared one.

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.