11

I've followed the lecture on Udemy. But I've got an error

here's my 'Inbox/contracts/Inbox.sol'

pragma solidity ^0.4.17;
contract Inbox {
    string public message;

    function Inbox(string initialMessage) public {
        message = initialMessage;
    }

    function setMessage(string newMessage) public {
        message = newMessage;
    }
}

and here is a 'Inbox/compile.js' file

const path = require('path');
const fs = require('fs');
const solc = require('solc');

const inboxPath = path.resolve(__dirname, 'contracts','Inbox.sol');
const source = fs.readFileSync(inboxPath, 'utf8');

console.log(solc.compile(source, 1));

and after I compile this file on terminal

node compile.js

got an error like below

  errors: 
           [ ':6:5: Warning: Defining constructors as functions with the same name 
as the contract is deprecated. Use "constructor(...) { ... }" 
    instead.\n    function Inbox(string initialMessage) public {\n    ^ 
    (Relevant source part starts here and spans across multiple lines).\n' ],

I use mac. One thing I did differently from the lecture was 'npm init' in other folder instead of Inbox and I coppied and pasted all the files created to inbox folder.

2
  • 1
    mirg's answer is close. But alant is correct, do not put the function keyword in front of the constructor keyword. Apr 22, 2018 at 20:39
  • in the previous version of solidity was correct. It has been changed recently and my post is updated now.
    – qbsp
    May 2, 2018 at 3:38

4 Answers 4

16

It should be a warning not an error. Anyway using this syntax

function Inbox(string initialMessage) public {
    message = initialMessage;
}

is deprecated in the newer version of Solidity. You have to use constructor instead of the contract name

constructor(string initialMessage) public {
    message = initialMessage;
}

this will compile without warnings

4
  • 1
    Also, if you're doing this, make sure you change the first line to pragma solidity ^0.4.22;, otherwise if someone tries to compile your contract using an old compiler it won't work as intended. Apr 18, 2018 at 3:09
  • But if you change the constructor function name from theContractName to constructor, then how can you deploy this contract from another contract??? How to fix this code after changing the constructor function name: address addr = new theContractName(..); ???
    – Russo
    Apr 19, 2018 at 8:43
  • in the same way you did before, just create a new instance with new
    – qbsp
    Apr 19, 2018 at 8:46
  • Solved here: ethereum.stackexchange.com/questions/46131/…
    – Russo
    Apr 19, 2018 at 12:02
3

Just use constructor() should be good.

constructor(string initialMessage) public {
    message = initialMessage;
}

If you use function constructor(...)... , I think you'll get the following warning:

Warning: This function is named "constructor" but is not the constructor of the contract. If you intend this to be a constructor, use "constructor(...) { ... }" without the "function" keyword to define it.

0

I personally just changed the compiler to solve the issue. Instead of a v4.17+ compiler, use the v4.17 compiler. Hope this helped :

0

Don't forget to add memory or storage when declare a parameters such as string in the constructor

 constructor(string memory initialMessage) {
        message = initialMessage;
    }

Your Answer

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

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