1

I have thoroughly read the documentation and isolated my problem down to a small set of sample code. I'm not sure where my problem is. I am trying to add multiple indices to a multi_index object so that I can sort in different ways. In this example, I have a default sort, and I'm trying to implement the "numa" and "numb" indices. Neither one is returning any results.

Here is the full HPP file:

#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>

using namespace eosio;
using std::string;

class scratch: public contract {
public: 
    scratch (account_name self) : contract (self) {}

    // @abi action
    void printtime (const string memo);

    // @abi action 
    void getdefault () ;

    // @abi action 
    void getnuma () ;

    // @abi action 
    void getnumb () ;

    // @abi action 
    void getnumc () ;

    // @abi action 
    void getnumd () ;

    // @abi action
    void additem (  const account_name  payer,
                    const uint64_t      numA,
                    const uint64_t      numB,
                    const uint64_t      numC,
                    const uint64_t      numD) ;

private:

    // @abi table items i64
    struct item {
        uint64_t    key;
        uint64_t    numA;
        uint64_t    numB;
        uint64_t    numC;
        uint64_t    numD;

        auto        primary_key() const {return key; }
        uint64_t    bynuma() const { return numA; }
        uint64_t    bynumb() const { return numB; }
        uint64_t    bynumc() const { return numC; }
        uint64_t    bynumd() const { return numD; }

        EOSLIB_SERIALIZE (item, (key)(numA)(numB)(numC)(numD));
    };

    typedef eosio::multi_index<N(items), item,
        indexed_by< N(numa),
            const_mem_fun<item, uint64_t, &item::bynuma>
        >,
        indexed_by< N(numb),
            const_mem_fun<item, uint64_t, &item::bynumb>
        >
    > item_table;

};

EOSIO_ABI(scratch, (printtime)(getdefault)(getnuma)(getnumb)(getnumc)(getnumd)(additem))

And here is the full CPP file:

  #include <scratch.hpp>

#include <eosiolib/eosio.hpp>
#include <eosiolib/asset.hpp>
#include <eosiolib/action.hpp>
#include <eosiolib/print.hpp>

#include <iostream>

using namespace std;

void scratch::printtime (const string memo) {
    print ("Time: ", now() );
    print ("Day Count: ", now() % 604800 / 60 / 60  );
    //print (now(), " : Now");
}

// prints all the items correctly
void scratch::getdefault () {
    print ("Items sorted by A\n");
    item_table item_t (_self, _self);
    auto item_itr = item_t.begin();

    while (item_itr != item_t.end()) {
        print ( " A=", item_itr->numA, ", B=", item_itr->numB, ", C=", item_itr->numC, ", D=", item_itr->numD, "\n");
        item_itr++;
    }
    print ("End of items");
}

// does not find any items
void scratch::getnuma ( ) {
    print ("Items sorted by A\n");
    item_table item_t (_self, _self);
    auto item_index = item_t.get_index< N(numa)>();
    auto item_itr = item_index.begin();

    while (item_itr != item_index.end()) {
        print ( " A=", item_itr->numA, ", B=", item_itr->numB, ", C=", item_itr->numC, ", D=", item_itr->numD, "\n");
        item_itr++;
    }
    print ("End of items");
}

// does not find any items
void scratch::getnumb () {

    item_table item_t (_self, _self);
    auto expidx = item_t.get_index<N(numb)>();

    print("Items sorted by B\n");
    for( const auto& item : expidx ) {
        print ( " A=", item.numA, ", B=", item.numB, ", C=", item.numC, ", D=", item.numD, "\n");
    }
    print ("End of items");
}


void scratch::getnumc () {}
void scratch::getnumd () {}

void scratch::additem ( const account_name  payer,
                        const uint64_t      numA,
                        const uint64_t      numB,
                        const uint64_t      numC,
                        const uint64_t      numD) {

    item_table it (_self, _self);
    it.emplace (payer, [&](auto& i) {
        i.key   = it.available_primary_key();
        i.numA  = numA;
        i.numB  = numB;
        i.numC  = numC;
        i.numD  = numD;
    });
 }
1
  • The code you wrote does not seem to be a problem. Can you upload the full source code? – smarteasy Jun 21 '18 at 1:59

Your Answer

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

Browse other questions tagged or ask your own question.