0

In the following, why is the constructor of C not inherited by CExt so that it can be used by another extending contract CExtChild?

contract C {
    uint id;
    constructor(uint _id) {
        id = _id;
    }
}

contract CExt is C { }

/*
error[7927]: TypeError: Wrong argument count for constructor call: 1 arguments given but expected 0. Remove parentheses if you do not want to provide arguments here.
 --> src/CBaseExt.sol:18:23:
*/
contract CExtChild is CExt(1) { }

2 Answers 2

2

Your code will work like this:

contract CExt is C { 
  constructor(uint value) C(value){}
}

contract CExtChild is CExt(1) { }

Hope it helps!

1

The error occurs because CExt's constructor has an empty function signature and cannot accept arguments. The constructor of C is inherited by CExt, however,CExt doesn't have its own constructor defined. If there is no constructor, the contract will assume the default constructor, which is equivalent to constructor() {}.

You should modify the CExt contract to accept the constructor argument and pass it to the base contract C:

pragma solidity ^0.8.0;

contract C {
    uint id;
    constructor(uint _id) {
        id = _id;
    }
}

contract CExt is C {
    constructor(uint _id) C(_id) { }
}


contract CExtChild is CExt(1) {}

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.