By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,162 Members | 1,099 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,162 IT Pros & Developers. It's quick & easy.

Trouble passing a function template into "for_each" algorithm

P: n/a
#include <iostream>
using namespace std;

#include <vector>
#include <string>
#include <fstream>
#include <algorithm>

template<class C>void PrintAll(C&v)
{
typename C::iterator i;
for (i = v.begin(); i!= v.end(); ++i)
{
cout << *i << endl;
}
}

void main()
{
const string s1 = "First line";
const string s2 = "Second line";
const string s3 = "Third line";

vector<string> V;

V.push_back(s1); V.push_back(s2); V.push_back(s3);

for_each(V.begin(),V.end(),PrintAll);
}

================================================== ===========
I won't bore you (yet) with the reams of complaint from MS VC++ when I
try to compile the above...

My question is:

What is the correct syntax to use "for_each" to call the function
template PrintAll ?

for_each(V.begin(),V.end(),PrintAll<const string>); ??
for_each(V.begin(),V.end(),PrintAll); ??

or is it even possible ?

Assistance and explanation would be welcomed on this.

Best regards,

Griff
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Griff" <gr*****@aol.com> wrote...
#include <iostream>
using namespace std;

#include <vector>
#include <string>
#include <fstream>
#include <algorithm>

template<class C>void PrintAll(C&v)
{
typename C::iterator i;
for (i = v.begin(); i!= v.end(); ++i)
{
cout << *i << endl;
}
}

void main()
int main()
{
const string s1 = "First line";
const string s2 = "Second line";
const string s3 = "Third line";

vector<string> V;

V.push_back(s1); V.push_back(s2); V.push_back(s3);

for_each(V.begin(),V.end(),PrintAll);
for_each(V.begin(), V.end(), PrintAll<string>);
}

================================================== ===========
I won't bore you (yet) with the reams of complaint from MS VC++ when I
try to compile the above...

My question is:

What is the correct syntax to use "for_each" to call the function
template PrintAll ?

for_each(V.begin(),V.end(),PrintAll<const string>); ??
for_each(V.begin(),V.end(),PrintAll); ??
Since 'PrintAll' is a template, and 'for_each' needs a function or
a functor, you need to _instantiate_ it: PrintAll<string>.

or is it even possible ?


Of course it is.

C++ Standard library's "string" class conforms to the Standard Container
requirements and that means it has the "iterator" type defined, has
"begin" and "end" members, so you can use it to iterate the characters
in it.

As to Visual C++, I don't know whether 6.0 will be able to compile this,
but I sincerely hope that 7.1 will.

Victor
Jul 22 '05 #2

P: n/a
Victor,

Thanks for that. Your solution was pretty much what I tried, but VC++
6.0 chokes on it. Glad to have independent confirmation that the
problem is with MS,
and not my understanding of the code (for once).

I'll try it again at work on monday, when I can use a decent system.
(Solaris and g++).

Cheers

- Griff
Jul 22 '05 #3

P: n/a
>
I'll try it again at work on monday, when I can use a decent system.
(Solaris and g++).


Yep, works beautifully on Solaris 8 with gcc 3.2...
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.