This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Compilation errors; bind2nd; ptr_fun
- To: "'gcc at gcc dot gnu dot org'" <gcc at gcc dot gnu dot org>
- Subject: Compilation errors; bind2nd; ptr_fun
- From: Anjul Srivastava <anjul dot srivastava at sanchez dot com>
- Date: Wed, 1 Mar 2000 14:33:25 -0500
How come the following program gives compilation errors while the one
following it does not? The difference between the two, as shown by the lines
with comments, is that in the former, the second argument to the function
add is passed by reference, while in the latter, a pointer to the second
argument is passed.
#include <algorithm>
#include <iostream>
#include <utility>
#include <string>
#include <map>
int main(void);
void add(string s, map< string, int > & counts) // difference
{
counts[s]++; // difference
}
int main(void)
{
map< string, int > counts;
for_each
(
istream_iterator<string>(cin),
istream_iterator<string>(),
bind2nd(ptr_fun(&add), counts) // difference
);
return 0;
}
#include <algorithm>
#include <iostream>
#include <utility>
#include <string>
#include <map>
int main(void);
void add(string s, map< string, int > * counts) // difference
{
(*counts)[s]++; // difference
}
int main(void)
{
map< string, int > counts;
for_each
(
istream_iterator<string>(cin),
istream_iterator<string>(),
bind2nd(ptr_fun(&add), &counts) // difference
);
return 0;
}
Thanks,
Anjul.