oggiemc
asked on
for_each function iterator
Hello all,
Im trying to run two similar programs..The first one runs fine but the second one gives the following error:
PersonProblem_09_3a.cpp: In function `int main()':
PersonProblem_09_3a.cpp:55 : error: no matching function for call to `for_each(__gnu_cxx::__nor mal_iterat or<int*, std::vector<int, std::allocator<int> > >,
Help appreciated,
Thanks
Im trying to run two similar programs..The first one runs fine but the second one gives the following error:
PersonProblem_09_3a.cpp: In function `int main()':
PersonProblem_09_3a.cpp:55
Help appreciated,
Thanks
************ Programme 1 OK **************
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
class Fish
{
string name;
public:
Fish(string);
virtual void display();
};
Fish::Fish(string nm): name(nm) {}
void Fish::display()
{
cout << "I am a fish and my name is " << name << endl;
}
void outputFunction(Fish x)
{
x.display();
}
int main(void)
{
string x;
vector<Fish> vect; // declare a vector container of Fish
cout << "Please enter five Fish:" << endl;
for(int i=0; i<5; i++){
cin >> x;
vect.push_back(Fish(x)); }
// loop through vector with for_each loop and execute
// outputFunction() for each element
for_each(vect.begin(), vect.end(), outputFunction);
}
********** Programme 2 Error *****************
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template <class T>
void outputFunction(T x)
{
cout << x << endl;
}
int main(void){
int x;
vector<int> vect; // declare a vector container of ints
cout << "Please enter a list of numbers:" << endl;
for (int i = 0; i < 5; ++i){
vect.push_back(x);}
sort(vect.begin(), vect.end()); // sort the array
cout << endl << "Sorted output of numbers:" << endl;
// loop through vector with for_each loop and execute
// outputFunction() for each element
for_each(vect.begin(), vect.end(), outputFunction);
}
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
>> Why does it work without parameters for user defined types?
It doesn't. It worked in the first program, because there, outputFunction was NOT a templated function.
It doesn't. It worked in the first program, because there, outputFunction was NOT a templated function.
ASKER
thanks alot..so when working with templated functions, its probably best to explicitly include the data type for the templated function?? how is it doen implicitly?? not that it really matters i guess..
Thanks again
Thanks again
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
ok, thanks for that..
ASKER
Do you always have to parameterise the function for primitive types?? Why does it work without parameters for user defined types?