0

I have following EOSIO smart contract:

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

using namespace eosio;

class random : public contract
{
public:
  using contract::contract;

  [[eosio::action]]
  void hi(name user) {
    print("Hello and welcome to the future ", user);
  }
  [[eosio::action]]
  int compute(int x)
  {
    // unsigned means no negative values which increases the range of numbers the variable can hold
    const unsigned int FIVE = 5;
    // FIVE = 3; // would throw an error as FIVE is declared constant
    return x * x + FIVE;
  }
};

EOSIO_DISPATCH(random, (compute))

I am not getting how to call compute action in EOSIO_DISPATCH(random, (compute)) because it has return type int. Can somebody help me with this? I want to deploy this smart contract on testnet. Following error is generated when I execute eosio-cpp -o random.wasm random.cpp --abigen:

random.cpp:62:1: error: no matching function for call to 'execute_action'
EOSIO_DISPATCH(random, (compute))
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/eosio.cdt/bin/../include/eosiolib/dispatcher.hpp:128:13: note: expanded from macro 'EOSIO_DISPATCH'
            EOSIO_DISPATCH_HELPER( TYPE, MEMBERS ) \
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/eosio.cdt/bin/../include/eosiolib/dispatcher.hpp:103:27: note: expanded from macro 'EOSIO_DISPATCH_HELPER'
   BOOST_PP_SEQ_FOR_EACH( EOSIO_DISPATCH_INTERNAL, TYPE, MEMBERS )
   ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/eosio.cdt/bin/../include/boost/preprocessor/seq/for_each.hpp:29:88: note: expanded from macro 'BOOST_PP_SEQ_FOR_EACH'
#    define BOOST_PP_SEQ_FOR_EACH(macro, data, seq) BOOST_PP_SEQ_FOR_EACH_DETAIL_CHECK(macro, data, seq)
                                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
note: (skipping 6 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
/usr/local/eosio.cdt/bin/../include/boost/preprocessor/seq/for_each.hpp:78:76: note: expanded from macro 'BOOST_PP_SEQ_FOR_EACH_M_IM'
#    define BOOST_PP_SEQ_FOR_EACH_M_IM(r, im) BOOST_PP_SEQ_FOR_EACH_M_I(r, im)
                                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
/usr/local/eosio.cdt/bin/../include/boost/preprocessor/seq/for_each.hpp:83:61: note: expanded from macro 'BOOST_PP_SEQ_FOR_EACH_M_I'
# define BOOST_PP_SEQ_FOR_EACH_M_I(r, macro, data, seq, sz) macro(r, data, BOOST_PP_SEQ_HEAD(seq))
                                                            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/eosio.cdt/bin/../include/eosiolib/dispatcher.hpp:98:7: note: expanded from macro 'EOSIO_DISPATCH_INTERNAL'
      eosio::execute_action( eosio::name(receiver), eosio::name(code), &OP::elem ); \
      ^~~~~~~~~~~~~~~~~~~~~
/usr/local/eosio.cdt/bin/../include/eosiolib/dispatcher.hpp:66:9: note: candidate template ignored: could not match 'void' against 'int'
   bool execute_action( name self, name code, void (T::*func)(Args...)  ) {
        ^
1 error generated.

1 Answer 1

1

There is some of the modification required in your contract to compile it.

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

using namespace eosio;

 class [[eosio::contract("random")]] random : public contract
{
  public:
  using contract::contract;

  [[eosio::action]]
  void hi(name user) {
    print("Hello and welcome to the future ", user);
  }
  [[eosio::action]]
  void compute(int x)
  {
    // unsigned means no negative values which increases the range of numbers the variable can hold
    const unsigned int FIVE = 5;
    // FIVE = 3; // would throw an error as FIVE is declared constant
    print( x * x + FIVE);
  }
};

EOSIO_DISPATCH(random, (compute))

And for query regarding return type have a look at this discussion and this.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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