2017-03-31 81 views
1

即时尝试编译一个学校作业,但即时通讯出错,并且由于在这个小时没有合格的老师可以在uni,我来这里寻求帮助。C++未能专注功能模板'iterator_traits'

即时得到一个错误,指出: “错误C2893无法专注函数模板 'iterator_traits < _Iter> :: difference_type的std ::距离(_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 

函数调用“:

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

你在哪里使用'select_randomly'。 – Jarod42

+1

这取决于用于实例化模板函数'seter'的类型'seter' t_randomly'。显示你对'select_randomly'的调用。 –

+0

顺便说一句,在输出窗格中,您应该有更长的错误消息。 – Jarod42

回答

1

你应该通过迭代器select_randomly,没有索引这里是正确的函数调用:std::vector<Move>::iterator it = select_randomly(possibleMoves.begin(), possibleMoves.end());要了解更多关于迭代器访问http://www.cprogramming.com/tutorial/stl/iterators.html

+0

我将如何使用此选择结果作为一种举动?移动m = possibleMoves [它]给出错误 – FrankK

+0

当然,因为迭代器不是索引。按照链接阅读如何正确使用它们。 –

+0

好的,如果你不想学习迭代器的东西,只需使用它像Move m = * it; –