Reputation: 511
im trying to compile a school assignment but im getting an error, and since there is no qualified teacher available at uni at this hour, i've come here for help.
im getting a error stating: "Error C2893 Failed to specialize function template 'iterator_traits<_Iter>::difference_type std::distance(_InIt,_InIt)'" line 22
i have no clue as to why this error occurs.
code:
#pragma once
// ttt.h
#ifndef TTT_H
#define TTT_H
#include <tuple>
#include <array>
#include <vector>
#include <ctime>
#include <random>
#include <iterator>
#include <iostream>
enum class Player { X, O, None };
using Move = int;
using State = std::array<Player, 9>;
// used to get a random element from a container
template<typename Iter, typename RandomGenerator>
Iter select_randomly(Iter start, Iter end, RandomGenerator& g) {
std::uniform_int_distribution<> dis(0, std::distance(start, end) - 1);
std::advance(start, dis(g));
return start;
}
template<typename Iter>
Iter select_randomly(Iter start, Iter end) {
static std::random_device rd;
static std::mt19937 gen(rd());
return select_randomly(start, end, gen);
}
std::ostream &operator<<(std::ostream &os, const State &state);
std::ostream &operator<<(std::ostream &os, const Player &player);
Player getCurrentPlayer(const State &state);
State doMove(const State &state, const Move &m);
Player getWinner(const State &state);
std::vector<Move> getMoves(const State &state);
#endif // TTT_H
function call":
State mcTrial(const State &board)
{
State currentBoard = board;
std::vector<Move> possibleMoves = getMoves(currentBoard);
while (possibleMoves.size() > 0) {
int move = select_randomly(0, (int)possibleMoves.size() -1);
Move m = possibleMoves[move];
currentBoard = doMove(currentBoard, m);
possibleMoves = getMoves(currentBoard);
}
return currentBoard;
}
Upvotes: 2
Views: 857
Reputation: 605
You should pass iterators to select_randomly
, not indexes. Here is the proper function call: std::vector<Move>::iterator it = select_randomly(possibleMoves.begin(), possibleMoves.end());
To learn more about iterators visit http://www.cprogramming.com/tutorial/stl/iterators.html
Upvotes: 1