c ++无法专门化功能模板' iterator_traits'

时间:2017-03-31 13:59:00

标签: c++ visual-studio

我正在尝试编写一份学校作业,但我得到了一个错误,而且由于此时大学没有合格的老师,我来这里寻求帮助。

我得到一个错误说明:"错误C2893无法专门化功能模板' iterator_traits< _Iter> :: difference_type std :: distance(_InIt,_InIt)'"第22行

我不清楚为什么会出现这种错误。

代码:

#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

函数调用&#34;:

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;
}

1 个答案:

答案 0 :(得分:1)

您应该将迭代器传递给select_randomly,而不是索引。以下是正确的函数调用:std::vector<Move>::iterator it = select_randomly(possibleMoves.begin(), possibleMoves.end());要了解有关迭代器的更多信息,请访问http://www.cprogramming.com/tutorial/stl/iterators.html