3

I am trying to call a contract from another contract.

first contract is the source of items like a data table. second contract is like a logic layer for the items.

I first deployed the contract1 and got the address, it is working fine.

When I tried to deploy the second contract which has the variable of type contract1 it is throwing the below error when compiling

 Error: Identifier not found or not unique. 

below is my second contact code

contract itemlistcallercontract {
     ItemListContract itemlistcontract;
     function itemlistcallercontract()
     {
     itemlistcontract = ItemListContract("0x16c5d0c8fccaf7e5824f5ae25c1662877cec6452");
     }

     function getitemcount() constant returns(int count)
     {
     return itemlistcontract.countItemList();
     }
}

i am trying to take the reference of contract1 by passing its address in the contract2 constructor.

But the error is thrown in the declaration of the itemlistcontract variable it self.

Cant we call a contract deploying them seperately? Is there any point I am missing here?

1

I refer to your code for ItemListContract in Unable to add new struct item into mapping or array.

The following code compiles without errors:

pragma solidity ^0.4.0;

contract ItemListContract {
    struct item {
        bytes iname;
        uint16 itemid;
        bytes icode;
        uint ivalue;
    }

    uint itemCount;
    mapping(bytes => item) itemList;
    item[] itemArray;

    function ItemListContract() {
        log0('hi');
    }

    function addItem(bytes name, uint16 iid, bytes code, uint val) {        
        var itemnew = item(name, iid ,code, val);
        // log0(itemnew);
        itemList[code] = itemnew;
        itemArray.push(itemnew);
        itemCount++;
    }

    function countItemList() constant returns (uint count) {     
        return itemCount;
    }

    function removeItem(bytes code) {
        delete itemList[code];
        itemCount--;
    }

    function getItem(bytes code) constant returns (bytes iname, uint val) {   
        return (itemList[code].iname, itemList[code].ivalue);
    }
}

contract ItemListCallerContract {

     ItemListContract itemListContract;

     function ItemListCallerContract() {
         itemListContract = ItemListContract(0x16c5d0c8fccaf7e5824f5ae25c1662877cec6452);
     }

     function getItemCount() constant returns(uint count) {
         return itemListContract.countItemList();
     }
}

In your code, the compiler will need to know the code for ItemListContract. The address for the first contract is defined without the double quotes. I also had to change the getItemCount() return type to match that of ItemListContract.countItemList().

You can also just provide the interface for the first contract without the implementation details as shown below:

pragma solidity ^0.4.0;

contract ItemListContract {

    function ItemListContract();

    function addItem(bytes name, uint16 iid, bytes code, uint val);

    function countItemList() constant returns (uint count);

    function removeItem(bytes code);

    function getItem(bytes code) constant returns (bytes iname, uint val);
}

contract ItemListCallerContract {

     ItemListContract itemListContract;

     function ItemListCallerContract() {
         itemListContract = ItemListContract(0x16c5d0c8fccaf7e5824f5ae25c1662877cec6452);
     }

     function getItemCount() constant returns(uint count) {
         return itemListContract.countItemList();
     }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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