Hey guys,
How would one create a vector of pointers to functions? I know from C to create an array of functions, but I'd like to learn how to do it with vectors as well. Here are my efforts so far:
#include <iostream>
#include <valarray>
#include <vector>
#include <ctime>
#include <cstdio>
using namespace std;
time_t startTime, nowTime, thenTime;
static const unsigned int SIZE = 1024 * 1024 * 5;
bool ruleone(int x);
bool ruletwo(int x);
int main (int argc, char *argv[]){
vector<bool*(int x)> rules;
rules.push_back(ruleone);
rules.push_back(ruletwo);
vector<int> numbers;
for(int i = 0; i < 35; i++){
numbers.push_back(i * 3);
}
for(int i = 0; i < 35; i++){
for(int n = 0; n < 2; n++){
if(rules[n](numbers[i])){
cout << numbers[i] << " passed rule " << n << endl;
}
}
}
return 0;
}
bool ruleone(int x){
if(x > 10) return true;
return false;
}
bool ruletwo(int x){
if(x > 50) return true;
return false;
}
That code won't compile with.. lots of errors:
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\allocator.h|81|instantiated from `std::allocator<bool*()(int)>'|
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\stl_vector.h|79|instantiated from `std::_Vector_base<bool*()(int), std::allocator<bool*()(int)> >::_Vector_impl'|
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\stl_vector.h|110|instantiated from `std::_Vector_base<bool*()(int), std::allocator<bool*()(int)> >'|
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\stl_vector.h|142|instantiated from `std::vector<bool*()(int), std::allocator<bool*()(int)> >'|
C:\Code\TEMP\main.cpp|17|instantiated from here|
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\ext\new_allocator.h|75|error: `const _Tp* __gnu_cxx::new_allocator<_Tp>::address(const _Tp&) const [with _Tp = bool*()(int)]' and `_Tp* __gnu_cxx::new_allocator<_Tp>::address(_Tp&) const [with _Tp = bool*()(int)]' cannot be overloaded|
C:\Code\TEMP\main.cpp||In function `int main(int, char**)':|
C:\Code\TEMP\main.cpp|18|error: no matching function for call to `std::vector<bool*()(int), std::allocator<bool*()(int)> >::push_back(bool (&)(int))'|
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\stl_vector.h|557|note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = bool*()(int), _Alloc = std::allocator<bool*()(int)>]|
C:\Code\TEMP\main.cpp|19|error: no matching function for call to `std::vector<bool*()(int), std::allocator<bool*()(int)> >::push_back(bool (&)(int))'|
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\stl_vector.h|557|note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = bool*()(int), _Alloc = std::allocator<bool*()(int)>]|
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\stl_vector.h||In destructor `std::_Vector_base<_Tp, _Alloc>::~_Vector_base() [with _Tp = bool*()(int), _Alloc = std::allocator<bool*()(int)>]':|
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\stl_vector.h|182|instantiated from `std::vector<_Tp, _Alloc>::vector(const typename std::_Vector_base<_Tp, _Alloc>::allocator_type&) [with _Tp = bool*()(int), _Alloc = std::allocator<bool*()(int)>]'|
C:\Code\TEMP\main.cpp|17|instantiated from here|
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\stl_vector.h|106|error: ISO C++ forbids using pointer to a function in subtraction|
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\stl_iterator.h||In member function `__gnu_cxx::__normal_iterator<_Iterator, _Container> __gnu_cxx::__normal_iterator<_Iterator, _Container>::operator+(const typename std::iterator_traits<_Iterator>::difference_type&) const [with _Iterator = bool*(*)(int), _Container = std::vector<bool*()(int), std::allocator<bool*()(int)> >]':|
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\stl_vector.h|462|instantiated from `typename _Alloc::reference std::vector<_Tp, _Alloc>::operator[](size_t) [with _Tp = bool*()(int), _Alloc = std::allocator<bool*()(int)>]'|
C:\Code\TEMP\main.cpp|29|instantiated from here|
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\stl_iterator.h|654|error: pointer to a function used in arithmetic|
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\stl_construct.h||In function `void std::__destroy_aux(_ForwardIterator, _ForwardIterator, __false_type) [with _ForwardIterator = bool*(*)(int)]':|
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\stl_construct.h|152|instantiated from `void std::_Destroy(_ForwardIterator, _ForwardIterator) [with _ForwardIterator = bool*(*)(int)]'|
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\stl_vector.h|256|instantiated from `std::vector<_Tp, _Alloc>::~vector() [with _Tp = bool*()(int), _Alloc = std::allocator<bool*()(int)>]'|
C:\Code\TEMP\main.cpp|17|instantiated from here|
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\stl_construct.h|120|error: ISO C++ forbids incrementing a pointer of type `bool*(*)(int)'|
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\ext\new_allocator.h||In member function `void __gnu_cxx::new_allocator<_Tp>::deallocate(_Tp*, size_t) [with _Tp = bool*()(int)]':|
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\stl_vector.h|117|instantiated from `void std::_Vector_base<_Tp, _Alloc>::_M_deallocate(_Tp*, size_t) [with _Tp = bool*()(int), _Alloc = std::allocator<bool*()(int)>]'|
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\stl_vector.h|106|instantiated from `std::_Vector_base<_Tp, _Alloc>::~_Vector_base() [with _Tp = bool*()(int), _Alloc = std::allocator<bool*()(int)>]'|
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\stl_vector.h|182|instantiated from `std::vector<_Tp, _Alloc>::vector(const typename std::_Vector_base<_Tp, _Alloc>::allocator_type&) [with _Tp = bool*()(int), _Alloc = std::allocator<bool*()(int)>]'|
C:\Code\TEMP\main.cpp|17|instantiated from here|
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\ext\new_allocator.h|86|error: invalid conversion from `bool*(*)(int)' to `void*'|
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\ext\new_allocator.h|86|error: initializing argument 1 of `void operator delete(void*)'|
||=== Build finished: 8 errors, 0 warnings ===|
How should I do this?
Thanks in Advance,
Nick