EOS.IO Asked by Saliya Perera on August 20, 2021
I tried the below code from a tutorial of a simple eos contract on a game but i get 10 errors while building it on eosstudio wasted a lot of time trying to figure the errors out but failed appreciate if anyone can help
Thank you
#include <mygame.hpp>
#include <string>
using namespace eosio;
using std::string;
class mygame : public contract {
using contract::contract;
public:
// mygame( name self ) : contract(self){}
///@abi action
void add(const name account, string& username, uint64_t level){
require_auth(account);
playerIndex players(_self,_self);
// address_index addresses(_self, _self.value);
auto iterator = players.find(account);
eosio_assert(iterator == players.end(), "Address of account already exists")
players.emplace(account,[&] (auto& player){
player.account_name = account;
player.username = username;
player.level = 1;
player.health = 100;
player.points = 0;
});
}
///@abi action
void update(const name account, uint64_t level, uint64_t health, uint64_t points){
require_auth(account);
playerIndex players(_self,_self);
auto iterator = players.find(account);
eosio_assert(iterator != players.end(), "Address of account does not exists")
players.modify(iterator, account, [&] (auto& player){
player.level = level;
player.points += points;
if((player.health - health) < 0){
player.health = 0;
}else{
player.health -= health;
}
});
}
///@abi action
void getplayer(const name account){
playerIndex players(_self,_self.value);
auto iterator = player.find(account);
eosio_assert(iterator != players.end(), "Address of account does not exists")
auto currentPlayer = players.get(account);
print("username : ", currentPlayer.username.c_str(), "level : ", currentPlayer.level, "helth : ", currentPlayer.health, "points : ", currentPlayer.points);
}
private:
///@abi table player uint64
struct player {
uint64_t account_name;
string username;
uint64_t level = 1;
uint64_t health = 100;
uint64_t points = 0;
uint64_t primary_key() const {return account_name;}
EOSLIB_SERIALIZE(player, (account_name)(username)(level)(health)(points))
};
typedef multi_index<N(player), player> playerIndex;
}
EOSIO_DISPATCH(mygame, (add)(update)(getplayer))
below are the errors shown
/project/src/mygame.cpp:84:25: error: 'player' does not refer to a value
typedef multi_index<N(player), player> playerIndex;
^
/project/src/mygame.cpp:71:10: note: declared here
struct player {
^
/project/src/mygame.cpp:18:17: error: excess elements in scalar initializer
playerIndex players(_self,_self);
^ ~~~~~~
/project/src/mygame.cpp:37:17: error: excess elements in scalar initializer
playerIndex players(_self,_self);
^ ~~~~~~
/project/src/mygame.cpp:58:17: error: excess elements in scalar initializer
playerIndex players(_self,_self.value);
^ ~~~~~~~~~~~~
/project/src/mygame.cpp:59:21: error: 'player' does not refer to a value
auto iterator = player.find(account);
^
/project/src/mygame.cpp:71:10: note: declared here
struct player {
^
/project/src/mygame.cpp:63:26: error: use of undeclared identifier 'currentPlayer'
print("username : ", currentPlayer.username.c_str(), "level : ", currentPlayer.level, "helth : ", currentPlayer.health, "points : ", currentPlayer.points);
^
/project/src/mygame.cpp:63:70: error: use of undeclared identifier 'currentPlayer'
print("username : ", currentPlayer.username.c_str(), "level : ", currentPlayer.level, "helth : ", currentPlayer.health, "points : ", currentPlayer.points);
^
/project/src/mygame.cpp:63:103: error: use of undeclared identifier 'currentPlayer'
print("username : ", currentPlayer.username.c_str(), "level : ", currentPlayer.level, "helth : ", currentPlayer.health, "points : ", currentPlayer.points);
^
/project/src/mygame.cpp:63:138: error: use of undeclared identifier 'currentPlayer'
print("username : ", currentPlayer.username.c_str(), "level : ", currentPlayer.level, "helth : ", currentPlayer.health, "points : ", currentPlayer.points);
^
/project/src/mygame.cpp:88:1: error: expected unqualified-id
EOSIO_DISPATCH(mygame, (add)(update)(getplayer))
^
/usr/opt/eosio.cdt/1.6.1/bin/../include/eosiolib/dispatcher.hpp:115:8: note: expanded from macro 'EOSIO_DISPATCH'
extern "C" {
^
10 errors generated.
Error while processing /project/src/mygame.cpp.
abigen error
Warning, empty ricardian clause file
Warning, empty ricardian clause file
This is the reference to the video tutorial
https://www.youtube.com/watch?v=cfbZfdGsGAE&list=PLL5pYVd8AWtSW4lHcWplRP6rTybxxj3yu&index=12
I tried to code something that I think it could help. In my implementation I don't use EOS studio but I manage to build my contract and put it on my local EOS blockchain. I am using two files: game.cpp and game.hpp.
//game.hpp
#include <eosio/eosio.hpp>
using namespace std;
using namespace eosio;
CONTRACT game : public contract {
private:
struct [[eosio::table]] player {
name account; // player account - primary key
std::string username; // player username
uint64_t level; // player level
uint64_t health; // player health
uint64_t points; // player points
uint64_t primary_key() const { return account.value; }
};
typedef multi_index<name("players"), player> players_table;
public:
game( name receiver, name code,
datastream<const char*> ds ) :
contract(receiver, code, ds)
{}
ACTION add(
name account,
std::string username,
uint64_t level,
uint64_t health,
uint64_t points,
);
};
//game.cpp
#include "game.hpp"
ACTION game::add(
name account,
std::string username,
uint64_t level,
uint64_t health,
uint64_t points) {
require_auth(account);
players_table players( get_self(), get_first_receiver().value );
auto player_iterator = players.find(account.value);
if (player_iterator == players.end()) {
players.emplace(account, [&]( auto& row ) {
row.account = account;
row.username = username;
row.level = level;
row.health = health;
row.points = points;
});
}
}
EOSIO_DISPATCH(game, (add))
Answered by Viktor Gagaleski on August 20, 2021
Probably the Version of eosio.cdt used by eossstudio is a lot more recent version than the one used in the video.
Try to use
C++11 Style Attributes
instead of the old notation-scheme.
Try to change the following:
class mygame : public contract {
to class [[eosio::contract]] mygame : public contract {
all ///@abi action
to [[eosio::action]]
and ///@abi table player uint64
to struct [[eosio::table]] player{
add a ;
after the last curly braket
You should follow the available and recent tutotials on developers.eos.io for a basic understanding about these notations. Don't use Video-tutorials older than half a year.
Answered by cmadh on August 20, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP