2014-05-25 94 views
2

当我使用boost/random/uniform_int_distribution.hpp,做我收到此错误:预期的类型说明符

boost::random::uniform_int_distribution<> dist(0, 5); 

我得到的错误是:

$ g++ game.cpp -std=c++11 -Wall -Ipath/to/boost -o game && ./game

game.cpp:11:20: error: expected type-specifier
game.cpp:11:20: error: expected '>'

为什么我得到这个错误?

注意:当我使用std::uniform_int_distribution<>时,我不会收到此错误。

这是引起问题的代码:

#include <boost/random/uniform_int_distribution.hpp> 

template< 
    class Engine = boost::default_random_engine 
> 
class game 
{ 
    boost::random::uniform_int_distribution<int> dist{0, 5}; 
}; 

int main() 
{ 
} 
+3

@JonathonReinhart它是,类模板有一个默认的类型参数“int”。 OP:请发布重现错误的[SSCCE](http://sscce.org)。孤立的单行代码是无用的,无论是gcc还是clang [编译没有错误](http://coliru.stacked-crooked.com/a/1233cfc5e28a9e55)。 – Praetorian

+0

@Praetorian更新。 – user2030677

+0

@Praetorian在Coliru上它说“没有类型名为'default_random_engine'在命名空间增强中”。那么'default_random_engine'的正确头文件是什么? – user2030677

回答

5

Boost.Random不定义default_random_engine类型。使用梅森捻线机,而不是直接(或它定义了other generators之一)

#include <boost/random/uniform_int_distribution.hpp> 
#include <boost/random/mersenne_twister.hpp> 

template< 
    class Engine = boost::mt19937 
> 
class game 
{ 
    boost::random::uniform_int_distribution<> dist{0, 5}; 
}; 

而且,由于这个问题被标记C++ 11,我会提的是,标准库也同时定义std::default_random_engine<random>标题中的std::uniform_int_distribution