Here is my echelleetserpent.hpp :
#include <vector>
#include <iostream>
#include <ctime>
#include <random>
#ifndef ECHELLE
#define ECHELLE
struct Player{
int position;
int n_step;
Player(): position(0), n_step(0) {};
};
class SnakesAndLadders{
private:
int n_players;
int n_board;
std::vector<Player> players;
std::vector<int> board;
public:
int roll_die(std::mt19937 &) const;
bool one_step(std::mt19937 & G, int j);
int game(std::mt19937);
};
#endif
And my echelleetserpent.cpp
#include "echelleetserpent.hpp"
#include <algorithm>
int SnakesAndLadders::roll_die(std::mt19937 & gen) const {
std::uniform_int_distribution<int> U(1,6);
int dice = U(gen);
return dice;
}
bool SnakesAndLadders::one_step(std::mt19937 & G, int j) {
int dice = roll_die(G);
std::cout << "Le résultat du dé est : " << dice << std::endl;
players[j].n_step++;
players[j].position+=dice;
return (players[j].position >= n_board) ? true : false;
}
int SnakesAndLadders::game(std::mt19937 G) {
while (std::any_of(players.begin(), players.end(), [n_board](Player p) {return p.position < n_board;})) {
for(int i=0; i<n_players; i++) {
this->one_step(G,i);
}
}
return std::min_element(this->players.begin(), this->players.end(), [n_board](Player p1, Player p2) {
if(p1.position>=n_board and p2.position >= n_board) { return p1.position < p2.position; } else { return false; }});
}
And i get the following error :
echelleetserpent.cpp: In member function ‘int SnakesAndLadders::game(std::mt19937)’:
echelleetserpent.cpp:60:54: error: capture of non-variable ‘SnakesAndLadders::n_board’
while (std::any_of(players.begin(), players.end(), [n_board](Player p) {return p.position < n_board;})) {
^~~~~~~
In file included from echelleetserpent.cpp:1:0:
echelleetserpent.hpp:18:7: note: ‘int SnakesAndLadders::n_board’ declared here
int n_board;
^~~~~~~
echelleetserpent.cpp: In lambda function:
echelleetserpent.cpp:60:94: error: ‘this’ was not captured for this lambda function
rs.begin(), players.end(), [n_board](Player p) {return p.position < n_board;})) {
^~~~~~~
echelleetserpent.cpp:60:94: error: invalid use of non-static data member ‘SnakesAndLadders::n_board’
In file included from echelleetserpent.cpp:1:0:
echelleetserpent.hpp:18:7: note: declared here
int n_board;
^~~~~~~
echelleetserpent.cpp: In member function ‘int SnakesAndLadders::game(std::mt19937)’:
echelleetserpent.cpp:65:71: error: capture of non-variable ‘SnakesAndLadders::n_board’
eturn std::min_element(this->players.begin(), this->players.end(), [n_board](Player p1, Player p2) {
^~~~~~~
In file included from echelleetserpent.cpp:1:0:
echelleetserpent.hpp:18:7: note: ‘int SnakesAndLadders::n_board’ declared here
int n_board;
^~~~~~~
echelleetserpent.cpp: In lambda function:
echelleetserpent.cpp:66:19: error: ‘this’ was not captured for this lambda function
if(p1.position>=n_board and p2.position >= n_board) { return p1.position < p2.position; } else { return false; }});
^~~~~~~
echelleetserpent.cpp:66:19: error: invalid use of non-static data member ‘SnakesAndLadders::n_board’
In file included from echelleetserpent.cpp:1:0:
echelleetserpent.hpp:18:7: note: declared here
int n_board;
^~~~~~~
echelleetserpent.cpp:66:46: error: ‘this’ was not captured for this lambda function
if(p1.position>=n_board and p2.position >= n_board) { return p1.position < p2.position; } else { return false; }});
^~~~~~~
echelleetserpent.cpp:66:46: error: invalid use of non-static data member ‘SnakesAndLadders::n_board’
In file included from echelleetserpent.cpp:1:0:
echelleetserpent.hpp:18:7: note: declared here
int n_board;
^~~~~~~
echelleetserpent.cpp: In member function ‘int SnakesAndLadders::game(std::mt19937)’:
echelleetserpent.cpp:66:116: error: cannot convert ‘__gnu_cxx::__normal_iterator<Player*, std::vector<Player> >’ to ‘int’ in return
ion >= n_board) { return p1.position < p2.position; } else { return false; }});
And the error continue :
In file included from /usr/include/c++/7/bits/stl_algobase.h:71:0, from /usr/include/c++/7/vector:60, from echelleetserpent.hpp:1, from echelleetserpent.cpp:1: /usr/include/c++/7/bits/predefined_ops.h: In instantiation of ‘bool __gnu_cxx::__ops::_Iter_pred<_Predicate>::operator()(_Iterator) [with _Iterator = __gnu_cxx::__normal_iterator >; _Predicate = SnakesAndLadders::game(std::mt19937)::]’: /usr/include/c++/7/bits/stl_algo.h:120:14: required from ‘_RandomAccessIterator std::__find_if(_RandomAccessIterator, _RandomAccessIterator, _Predicate, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator >; _Predicate = __gnu_cxx::__ops::_Iter_pred
]’ /usr/include/c++/7/bits/stl_algo.h:161:23: required from ‘_Iterator std::__find_if(_Iterator, _Iterator, _Predicate) [with _Iterator = __gnu_cxx::__normal_iterator >; _Predicate = __gnu_cxx::__ops::_Iter_pred ]’ /usr/include/c++/7/bits/stl_algo.h:3932:28: required from ‘_IIter std::find_if(_IIter, _IIter, _Predicate) [with _IIter = __gnu_cxx::__normal_iterator >; _Predicate = SnakesAndLadders::game(std::mt19937)::]’ /usr/include/c++/7/bits/stl_algo.h:526:47: required from ‘bool std::none_of(_IIter, _IIter, _Predicate) [with _IIter = __gnu_cxx::__normal_iterator >; _Predicate = SnakesAndLadders::game(std::mt19937)::]’ /usr/include/c++/7/bits/stl_algo.h:544:27: required from ‘bool std::any_of(_IIter, _IIter, _Predicate) [with _IIter = __gnu_cxx::__normal_iterator >; _Predicate = SnakesAndLadders::game(std::mt19937)::]’ echelleetserpent.cpp:60:103: required from here /usr/include/c++/7/bits/predefined_ops.h:283:11: error: void value not ignored as it ought to be { return bool(_M_pred(*__it)); }
Aucun commentaire:
Enregistrer un commentaire