2014-05-25 89 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