0

I want my contract to inherit another contract, the issue is that the contract uses a struct inside its constructor, and I'm not sure how to pass that data type from my contract.

Method 1

Contract I want to inherit:

contract Parent {
    struct CustomType {
        uint x;
        bool y;
        string z;
    }

    constructor(CustomType memory customType) {

    }
}

My contract:

contract Child is Parent{
    struct CustomType {
        uint x;
        bool y;
        string z;
    }

    constructor(CustomType memory customType) Parent(customType) {}
}

The error:

TypeError: Invalid type for argument in modifier invocation. Invalid implicit conversion from struct Child.CustomType memory to struct Parent.CustomType memory requested.

Method 2

Importing the struct into the file and using it also doesn't work:

import {Parent, CustomType} from "./Inherit.sol";

contract Child is Parent{

    constructor(CustomType memory customType) Parent(customType) {}
}

The error:

DeclarationError: Declaration "CustomType" not found in "contracts/Inherit.sol"

1 Answer 1

0

When inheriting a contract that uses a custom data type inside its constructor, you must reference that data type in your child contract's constructor.

For example:

contract Parent {
    struct CustomType {
        uint x;
        bool y;
        string z;
    }

    constructor(CustomType memory customType) {}
}
contract Child is Parent{
    constructor(Parent.CustomType memory customType) Parent(customType) {}
}

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.