I created a foreach() function, and it should print values of an array, but it tells me this:
no matching function for call to 'foreach(std::array<int, 4>&, void(&)(int))'
And also:
mismatched types 'unsigned int' and 'long unsigned int'
But when I try to use vectors instead of arrays, or on line 11 use template<unsigned int N> instead of unsigned int, if I use long unsigned int, it works fine.
So, why do I need to use long unsigned int?
And what does the "no matching function" error mean with arrays?
#include<iostream> #include<string> #include<array> typedef void(*func)(int); void print(int value) { std::cout << "value is : " << value << std::endl; } template<unsigned int N> void foreach(std::array<int, N>& values, func print) { int value; for(int i = 0; i < values.size(); i++) { value = values[i]; print(value); } } int main() { std::array<int, 4> arr = { 0, 1, 2, 3 }; foreach(arr, print); return 0; } With vectors:
#include<iostream> #include<string> #include<vector> typedef void(*func)(int); void print(int value) { std::cout << "value is : " << value << std::endl; } void foreach(std::vector<int>& values, func print) { int value; for(int i = 0; i < values.size(); i++) { value = values[i]; print(value); } } int main() { std::vector<int> v = { 0, 1, 2, 3 }; foreach(v, print); return 0; }
std::for_each()? And if it's the pair of iterators, why not just wrap it?template<size_t N>seems like a better fit forvoid foreach