#include <algorithm> #include <functional> #include <iostream> #include <cstdlib> #include <ctime> #include <vector>
using namespace std; class RandomGenObj { public: RandomGenObj() { srand(static_cast<int>(time(0))); } int operator()(int remainder) const { return rand() % remainder; } }; int main() { vector<int> v1(10, 10); RandomGenObj rg(); random_shuffle(v1.begin(), v1.end(), rg); for (vector<int>::const_iterator iter = v1.begin(); iter != v1.end(); ++iter) cout << *iter << ' ' << endl; cout << endl; } Above is the test program, I compiled it under gcc version 4.0.0 20050519(Red Hat 4.0.0-8), and the compiler output as follow: /usr/lib/gcc/i386-redhat-linux/4.0.0/../../../../include/c ++/4.0.0/bits/stl_algo.h: In function ‘void std::random_shuffle(_RandomAccessIterator, _RandomAccessIterator, _RandomNumberGenerator&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, _RandomNumberGenerator = RandomGenObj ()()]’: random_shuffle.cpp:23: instantiated from here /usr/lib/gcc/i386-redhat-linux/4.0.0/../../../../include/c ++/4.0.0/bits/stl_algo.h:1793: error: too many arguments to function /usr/lib/gcc/i386-redhat-linux/4.0.0/../../../../include/c ++/4.0.0/bits/stl_algo.h:1793: error: no match for ‘operator+’ in ‘__first + (+ __rand)()’ /usr/lib/gcc/i386-redhat-linux/4.0.0/../../../../include/c ++/4.0.0/bits/stl_iterator.h:653: note: candidates are: __gnu_cxx::__normal_iterator<_Iterator, _Container> __gnu_cxx::__normal_iterator<_Iterator, _Container>::operator+(const typename std::iterator_traits<_Iterator>::difference_type&) const [with _Iterator = int*, _Container = std::vector<int, std::allocator<int> >] /usr/lib/gcc/i386-redhat-linux/4.0.0/../../../../include/c ++/4.0.0/bits/stl_bvector.h:267: note: std::_Bit_iterator std::operator+(ptrdiff_t, const std::_Bit_iterator&) /usr/lib/gcc/i386-redhat-linux/4.0.0/../../../../include/c ++/4.0.0/bits/stl_bvector.h:353: note: std::_Bit_const_iterator std::operator+(ptrdiff_t, const std::_Bit_const_iterator&) however, if I rewrite the second line in function main as follow: RandomGenObj rg; Then the compiler pass the program, everything is right. due to my limit knowledge in C++, I can't assure if it's due to compiler's own problem. I think in syntax there's no difference between "RandomGenObj rg;" and "RandomGenObj rg()", since it has a default constructor. I want to know if this is a compiler's error, and because I don't use the lastest gcc compiler version, I want to know if in latest version the problem has gone away.