0

So I created a modifier to make it so for certain functions only the deployer of the contract can use the function. But once I created it, I'm getting an undeclared identifier error. Heres my constructor:

constructor() public{
    AdopterID =0; //increments 1 every time there is a new adopter addded
    DogID=0; //increments 1 everytime there is a new dog added
    address owner = msg.sender;
}

and heres my modifier that is throwing the error:

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

Can anyone tell me whats wrong? It looks basic enough that there shouldnt be any errors.

1
  • You are creating a local variable owner in your constructor. That's why the msg.sender is not assigned into your stored variable owner. Just remove the address type in front of the owner variable and you should be fine.
    – ashhanai
    Nov 7, 2022 at 17:33

2 Answers 2

0

You need to instantiate the owner outside of your constructor and assign it in your constructor.

address public owner;

constructor() public{
    AdopterID =0; //increments 1 every time there is a new adopter addded
    DogID=0; //increments 1 everytime there is a new dog added
    owner = msg.sender;
}
0

constructor() public{ AdopterID =0; //increments 1 every time there is a new adopter addded DogID=0; //increments 1 everytime there is a new dog added address owner = msg.sender; }

you're declaring owner variable in constructor scope. and if you wanted to use owner anywhere in contract then declare owner in global scope.

/////////////////

address owner; constructor() public{ AdopterID =0; //increments 1 every time there is a new adopter addded DogID=0; //increments 1 everytime there is a new dog added owner = msg.sender; }

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.