我正在练习如何在EOS中编写智能合约,并且试图构建我的tic tac toe版本,以EOS开发人员门户网站为例。
但是,由于某种原因,当我尝试生成.wast文件时,出现以下错误: “ test.cpp:173:1:错误:隐式删除了'test'从基类'contract'继承的构造函数”。
在此问题开始发挥作用之前,由于我没有Ricardian合同而被拒绝构建abi文件。但是,EOS开发人员门户中的井字游戏示例也没有。...
(所有代码都是.cpp文件)
#include <eosiolib/eosio.hpp>
#include <stdint.h>
using namespace eosio;
class test : public eosio::contract{
public:
using contract::contract;
test(account_name challenger_id, account_name host_id) :
contract(challenger_id), games_table(_self, _self) { }
bool board_is_full(uint32_t const board[9]) {
for (int a = 0; a <= 8; a++) {
if (board[a] != 0)
continue;
else
return false;
}
return true;
}
bool is_valid_move(uint8_t loc, uint32_t const board[9]) {
if (board[loc] == 0)
return true;
else
return false;
}
bool three_in_a_row(uint32_t const board[9], int num) {
for (int a = 0; a <= 2; a++) {
for (int b = 0; b <= 6; b += 3) {
if (board[a + b] == num) {
if (b == 6) {
return true;
}
continue;
}
break;
}
}
for (int a = 0; a <= 2; a+=2) {
if(a==0) {
for (int b = 0; b <= 8; b += 4) {
if (board[a + b] == num) {
if (b == 8) {
return true;
}
continue;
}
else {
break;
}
}
}
else{
for(int b = 0; b<=4; b++) {
if (board[a + b] == num) {
if (b == 4) {
return true;
}
continue;
}
else {
break;
}
}
}
}
return false;
}
int winner_scanner(uint32_t const board[9]) {
if (three_in_a_row(board, 1)) {
return 1;
}
if (three_in_a_row(board, 2)) {
return 2;
}
return 0;
}
///@abi action
void create(account_name challenger, account_name host) {
auto itr = games_table.find(challenger);
eosio_assert(static_cast<uint32_t>(itr != games_table.end()), "game already exists");
games_table.emplace(challenger, [&](auto& g) {
g.challenger = challenger;
g.host = host;
g.turn = host;
});
}
///@abi action
void place(account_name challenger, account_name turn, int loc){
auto itr = games_table.find(challenger);
require_auth(turn);
require_auth(itr->challenger);
eosio_assert(games_table.end() == itr, "game does not exist");
eosio_assert(itr->turn == turn, "not this player's turn");
eosio_assert(!board_is_full(itr->board), "board is full");
eosio_assert(is_valid_move(loc, itr->board), "invalid move");
games_table.modify(itr, challenger, [&](auto& g){
if(challenger == turn){
g.turn = g.host;
g.board[loc] = 2;
}
else{
g.turn = g.challenger;
g.board[loc] = 1;
}
});
int winner = winner_scanner(itr->board);
if (winner != 0) {
if (winner == 2) {
eosio_assert(true, "Challenger wins!");
}
if (winner == 1) {
eosio_assert(true, "Host wins!");
}
}
}
///@abi action
void restart(account_name challenger){
auto itr = games_table.find(challenger);
eosio_assert(itr!=games_table.end(), "game doesn't exist");
games_table.modify(itr, challenger, [&](auto& g){
for(int a= 0; a<=8; a++){
g.board[a] = 0;
}
});
}
///@abi action
void forfeit(account_name challenger, account_name forfeiter){
auto itr = games_table.find(challenger);
eosio_assert(itr==games_table.end(), "game does not exist");
games_table.erase(itr);
}
private:
///@abi table
struct game{
account_name challenger;
account_name host;
account_name turn;
uint32_t board[9];
account_name primary_key() const {return challenger;}
EOSLIB_SERIALIZE(game, (challenger)(host)(turn))
};
multi_index<N(games_table), game> games_table;
};
EOSIO_ABI(test, (create)(place)(restart)(forfeit))
答案 0 :(得分:0)
首先要生成一个abi,您需要一个Ricardian合同。 如果只想执行tic_tac_toe,只需从hello示例中获取Ricardian合同并更改其名称。
对于其他错误,也许您需要删除
using contract::contract;