calling method failed in another contract when create an instance by new

129 views Asked by At

i'm trying to use a contract instance as a variable of another contract, such as the example below.

pragma solidity ^0.4.23;

contract basic {
    uint num1 = 10;
    
    function getNum1() public view returns(uint) {
        return num1;
    }
    function setNum1(uint _num) public returns(uint) {
         num1 = _num;
    }
}

contract parent {
    uint public num2;
    basic public b;

    constructor() public {
        b = new basic();
        num2 = 20;
    }
        
    function getNum1() public constant returns(uint) {
        return b.getNum1();
    }
    
}

while when i test the contract in remix and truffle , it worked well. enter image description here but util i deployed the contract "parent" on my private network, parent.getNum1() returned '0' instead of '10' as supposed.

further more, i tried other type of constructors such as take an address of 'basic' as a parameter, it didn't work as well.

i also tried some contracts thats takes another contract instance as a variable, they all didn't work well on private network.

does anybody ever meet this problem? help!!!

1

There are 1 answers

0
Shawn yang On

coming to close the question now ! i deployed my contract on ropsten test network, and the contract worked well. it seems that my private network didn't support the usage of calling from another contract. anybody interested can have a try to see.