23

What does this warning actually mean and how to resolve this ?

enter image description here

1
  • Can you also upload the code and which solidity version you are using?
    – Anupam
    May 7, 2021 at 10:06

4 Answers 4

30

This is saying that adding public - a visibility specifier - to a constructor has no effect.

Constructors are run only once, when the contract is initially deployed. They can't be called at a later time, so aren't "visible" in the sense that variables or functions are.

Abstract contracts don't have a constructor, so the error is basically saying: "You've incorrectly used a visibility specifier on a constructor - did you even mean to use a constructor in the first place, or did you intend to write an abstract contract?".

3
  • Oh okay so basically constructors can't have any 'visibility specifier' May 7, 2021 at 10:26
  • That's correct - there's no need for them to have one. May 7, 2021 at 11:24
  • i am using v0.8.0 and abstract contract not giving any error while defining construtor. pragma solidity ^0.8.7; abstract contract Inheritance { constructor() { } } Jul 9 at 7:21
10

remove public from constructor, that's all.

eg.- constructor( . . . ) { . . . }

why?

its constructor, not a function. So solidity compiler says : "u don't need to make constructor public, its constructor to run one time, not a function to be made public and call again and again"

5

In v0.5.0 it was mandatory to add visibility to the constructor. This is from the docs of the breaking changes in v0.5.0: https://docs.soliditylang.org/en/v0.7.0/050-breaking-changes.html#explicitness-requirements

Explicit function visibility is now mandatory. Add public to every function and constructor, and external to every fallback or interface function that does not specify its visibility already

I guess it wasn't mandatory before but best practice.

Things have changed since v0.7.0: https://docs.soliditylang.org/en/v0.7.0/070-breaking-changes.html#functions-and-events

Visibility (public / external) is not needed for constructors anymore: To prevent a contract from being created, it can be marked abstract. This makes the visibility concept for constructors obsolete.

2
  • 1
    I'm a teenager in solidity, and met this issue when I try to change the solidity version from 0.5.0 to 0.8.0. This answer made me clear to understand the why. Thanks!
    – Bi Wu
    Dec 21, 2021 at 20:07
  • 1
    Haha actually I wrote the answer on my first day of learning solidity. Literally just take 3-4 days off and read the whole docs. You won't become an instant pro, and you'll eventually forget most of the things. But you brain will store some smaller things that will help you find your solution in the docs. Dec 23, 2021 at 16:53
1

Encountered same issue with > 0.7.0. Remove the public identifier.

Like this:

constructor (string memory message) {
    requestor = msg.sender;
    requestMessage = message;
}

Constructors are run once, so the identifier has no effect.

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