2

I am using Ubuntu 18.04 LTS first of all.

So I am trying to look up how to start building an EOS dApp, starting with this example on Github.

Here is the link: https://github.com/eosasia/eos-todo

I cloned the repository and tried to run it by deploying the todo.cpp file which is inside the contract folder.

The whole code for todo.cpp is here:

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

class todo_contract : public eosio::contract {
  public:
    todo_contract(account_name self)
      :eosio::contract(self),
      todos(_self, _self)
      {}

    // @abi action
    void create(account_name author, const uint32_t id, const std::string& description) {
      todos.emplace(author, [&](auto& new_todo) {
        new_todo.id  = id;
        new_todo.description = description;
        new_todo.completed = 0;
      });

      eosio::print("todo#", id, " created");
    }

    // @abi action
    void destroy(account_name author, const uint32_t id) {
      auto todo_lookup = todos.find(id);
      todos.erase(todo_lookup);

      eosio::print("todo#", id, " destroyed");
    }

    // @abi action
    void complete(account_name author, const uint32_t id) {
      auto todo_lookup = todos.find(id);
      eosio_assert(todo_lookup != todos.end(), "Todo does not exist");

      todos.modify(todo_lookup, author, [&](auto& modifiable_todo) {
        modifiable_todo.completed = 1;
      });

      eosio::print("todo#", id, " marked as complete");
    }

  private:
    // @abi table todos i64
    struct todo {
      uint64_t id;
      std::string description;
      uint64_t completed;

      uint64_t primary_key() const { return id; }
      EOSLIB_SERIALIZE(todo, (id)(description)(completed))
    };

    typedef eosio::multi_index<N(todos), todo> todo_table;
    todo_table todos;
};

EOSIO_DISPATCH(todo_contract, (create)(complete)(destroy))

I tried to build wast and abi file from it for the first step but there are 4 errors coming up when I type

eosio-cpp -o todo.wast todo.cpp && eosio-cpp -g todo.abi todo.cpp

in the Terminal.

The 4 errors I have got are:

todo.cpp:57:40: error: unknown type name 'complete'
EOSIO_DISPATCH(todo_contract, (create)(complete)(destroy))
                                       ^
todo.cpp:57:50: error: unknown type name 'destroy'
EOSIO_DISPATCH(todo_contract, (create)(complete)(destroy))
                                                 ^
todo.cpp:57:32: error: C++ requires a type specifier for all declarations
EOSIO_DISPATCH(todo_contract, (create)(complete)(destroy))
                               ^
todo.cpp:57:59: error: expected function body after function declarator
EOSIO_DISPATCH(todo_contract, (create)(complete)(destroy))
                                                          ^
4 errors generated.

I have googled many times but could not find the solution yet.

Any help I can use about this problem? Thanks in advance!

todo.cpp:9:10: error: no matching constructor for initialization of
      'eosio::contract'
        :eosio::contract(receiver, code, ds),
         ^               ~~~~~~~~~~~~~~~~~~
/usr/local/eosio.cdt/bin/../include/eosiolib/contract.hpp:26:7: note: candidate
      constructor not viable: requires single argument 'n', but 3 arguments were
      provided
      contract( account_name n ):_self(n){}
      ^
/usr/local/eosio.cdt/bin/../include/eosiolib/contract.hpp:18:7: note: candidate
      constructor (the implicit copy constructor) not viable: requires 1
      argument, but 3 were provided
class contract {
      ^
/usr/local/eosio.cdt/bin/../include/eosiolib/contract.hpp:18:7: note: candidate
      constructor (the implicit move constructor) not viable: requires 1
      argument, but 3 were provided
todo.cpp:10:27: error: member reference base type 'account_name' (aka
      'unsigned long long') is not a structure or union
        todos(_self, _self.value)
                     ~~~~~^~~~~~
todo.cpp:58:40: error: unknown type name 'complete'
EOSIO_DISPATCH(todo_contract, (create)(complete)(destroy))
                                       ^
todo.cpp:58:50: error: unknown type name 'destroy'
EOSIO_DISPATCH(todo_contract, (create)(complete)(destroy))
                                                 ^
todo.cpp:58:32: error: C++ requires a type specifier for all declarations
EOSIO_DISPATCH(todo_contract, (create)(complete)(destroy))
                               ^
todo.cpp:58:59: error: expected function body after function declarator
EOSIO_DISPATCH(todo_contract, (create)(complete)(destroy))
                                                          ^
6 errors generated.

These 6 errors are the new errors I have got right now.

  • Check to make sure you are using eosio-cpp. The class eosio::contract with constructor contract(account_name n) belong to eosiocpp, deprecated old compiler. Have you installed eosio.cdt? – conr2d Jan 1 at 12:44
1

Not only changing EOSIO_ABI macro to EOSIO_DISPATCH, there are some more to be changed.

  • account_name is removed. Use eosio::name.
  • Constructor requires three parameters, receiver, code and ds.
  • multi_index table needs integer as second parameter instead of name.
  • Use [[eosio::xxx]] attribute instead of /// @abi
  • N() macro is removed. Use "blahblah"_n operator instead.

--

#include <eosiolib/eosio.hpp>

using eosio::name;
using eosio::datastream;

class todo_contract : public eosio::contract {
public:
    todo_contract(name receiver, name code, datastream<const char*> ds)
        :eosio::contract(receiver, code, ds),
        todos(_self, _self.value)
        {}

    [[eosio::action]]
    void create(name author, const uint32_t id, const std::string& description) {
        todos.emplace(author, [&](auto& new_todo) {
        new_todo.id  = id;
        new_todo.description = description;
        new_todo.completed = 0;
        });

        eosio::print("todo#", id, " created");
    }

    [[eosio::action]]
    void destroy(name author, const uint32_t id) {
        auto todo_lookup = todos.find(id);
        todos.erase(todo_lookup);

        eosio::print("todo#", id, " destroyed");
    }

    [[eosio::action]]
    void complete(name author, const uint32_t id) {
        auto todo_lookup = todos.find(id);
        eosio_assert(todo_lookup != todos.end(), "Todo does not exist");

        todos.modify(todo_lookup, author, [&](auto& modifiable_todo) {
        modifiable_todo.completed = 1;
        });

        eosio::print("todo#", id, " marked as complete");
    }

private:
    struct [[eosio::table]] todo {
        uint64_t id;
        std::string description;
        uint64_t completed;

        uint64_t primary_key() const { return id; }
        EOSLIB_SERIALIZE(todo, (id)(description)(completed))
    };

    typedef eosio::multi_index<"todos"_n, todo> todo_table;
    todo_table todos;
};

EOSIO_DISPATCH(todo_contract, (create)(complete)(destroy))
  • Hey. Thanks for your help, but I still get errors. It is too long to leave as a comment, so I will edit my original post to show. Mind helping me a little more? – aion-alpha Dec 31 '18 at 13:27
  • I am not friendly with C++, maybe that is why I am struggling so hard. I will be really appreciated if you could help me just a little bit more! – aion-alpha Dec 31 '18 at 13:32

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.