Compilation errors; bind2nd; ptr_fun

Anjul Srivastava anjul.srivastava@sanchez.com
Wed Mar 1 11:34:00 GMT 2000


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.


More information about the Gcc mailing list