question about local class in a function

Discussion in 'C++' started by Nan Li, Feb 17, 2008.

  1. Nan Li

    Nan Li Guest

    Hello,
    Can any one explain why the following code cannot get compiled ??
    Thanks.

    #include <iostream>
    #include <string>
    #include <vector>
    #include <iterator>
    #include <algorithm>

    using namespace std;

    int main(int argc, char* argv[])
    {
    struct A
    {
    void operator()(int i)
    {
    cout << i << endl;
    }
    };

    vector<int> v(3);

    for_each( v.begin(), v.end(), A() );

    return 0;
    }

    c.cpp: In function 'int main(int, char**)':
    c.cpp:21: error: no matching function for call to
    'for_each(__gnu_cxx::__normal_iterator<int*, std::vector<int,
    std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*,
    std::vector<int, std::allocator<int> > >, main(int, char**)::A)'
    Nan Li, Feb 17, 2008
    #1
    1. Advertising

  2. Nan Li

    Ian Collins Guest

    Nan Li wrote:
    > Hello,
    > Can any one explain why the following code cannot get compiled ??
    > Thanks.
    >
    > #include <iostream>
    > #include <string>
    > #include <vector>
    > #include <iterator>
    > #include <algorithm>
    >
    > using namespace std;
    >
    > int main(int argc, char* argv[])
    > {
    > struct A
    > {
    > void operator()(int i)
    > {
    > cout << i << endl;
    > }
    > };
    >
    > vector<int> v(3);
    >
    > for_each( v.begin(), v.end(), A() );
    >
    > return 0;
    > }
    >
    > c.cpp: In function 'int main(int, char**)':
    > c.cpp:21: error: no matching function for call to
    > 'for_each(__gnu_cxx::__normal_iterator<int*, std::vector<int,
    > std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*,
    > std::vector<int, std::allocator<int> > >, main(int, char**)::A)'
    >

    gcc trying to tell you in its own obscure way that you can't use a local
    type as a template argument.

    --
    Ian Collins.
    Ian Collins, Feb 17, 2008
    #2
    1. Advertising

  3. Nan Li

    Salt_Peter Guest

    On Feb 17, 2:55 am, Nan Li <> wrote:
    > Hello,
    > Can any one explain why the following code cannot get compiled ??
    > Thanks.
    >
    > #include <iostream>
    > #include <string>
    > #include <vector>
    > #include <iterator>
    > #include <algorithm>
    >
    > using namespace std;
    >
    > int main(int argc, char* argv[])
    > {
    > struct A
    > {
    > void operator()(int i)
    > {
    > cout << i << endl;
    > }
    > };
    >
    > vector<int> v(3);
    >
    > for_each( v.begin(), v.end(), A() );
    >
    > return 0;
    >
    > }
    >
    > c.cpp: In function 'int main(int, char**)':
    > c.cpp:21: error: no matching function for call to
    > 'for_each(__gnu_cxx::__normal_iterator<int*, std::vector<int,
    > std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*,
    > std::vector<int, std::allocator<int> > >, main(int, char**)::A)'


    Your operator expects a parameter.
    Since you'ld end up using a placeholder...
    Why not just use boost::lambda

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include "boost/lambda/lambda.hpp"

    int main()
    {
    using boost::lambda::_1;
    std::vector< int > v(3, 9);
    std::for_each( v.begin(), v.end(), std::cout << _1 << '\n' );
    }

    /*
    9
    9
    9
    */

    http://www.boost.org/doc/html/lambda.html
    Salt_Peter, Feb 17, 2008
    #3
  4. Nan Li

    Jerry Coffin Guest

    In article <33e8504b-bca1-411f-bf19-b619a4846c67
    @q70g2000hsb.googlegroups.com>, says...
    > Hello,
    > Can any one explain why the following code cannot get compiled ??
    > Thanks.
    >
    > #include <iostream>
    > #include <string>
    > #include <vector>
    > #include <iterator>
    > #include <algorithm>
    >
    > using namespace std;
    >
    > int main(int argc, char* argv[])
    > {
    > struct A
    > {
    > void operator()(int i)
    > {
    > cout << i << endl;
    > }
    > };
    >
    > vector<int> v(3);
    >
    > for_each( v.begin(), v.end(), A() );
    >
    > return 0;
    > }


    Local types don't have linkage, so they can't be used as template
    parameters.

    Fortunately, the entire type and for_each that uses it work out to:

    std::copy(v.begin(), v.end(),
    std::eek:stream_iterator<int>(std::cout, "\n"));

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Feb 17, 2008
    #4
  5. Nan Li

    Jim Langston Guest

    Nan Li wrote:
    > Hello,
    > Can any one explain why the following code cannot get compiled ??
    > Thanks.
    >
    > #include <iostream>
    > #include <string>
    > #include <vector>
    > #include <iterator>
    > #include <algorithm>
    >
    > using namespace std;
    >
    > int main(int argc, char* argv[])
    > {
    > struct A
    > {
    > void operator()(int i)
    > {
    > cout << i << endl;
    > }
    > };
    >
    > vector<int> v(3);
    >
    > for_each( v.begin(), v.end(), A() );
    >
    > return 0;
    > }
    >
    > c.cpp: In function 'int main(int, char**)':
    > c.cpp:21: error: no matching function for call to
    > 'for_each(__gnu_cxx::__normal_iterator<int*, std::vector<int,
    > std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*,
    > std::vector<int, std::allocator<int> > >, main(int, char**)::A)'


    Microsoft C++ .net 2003 gives a better error:
    error C2918: '(int,char *[])main::A' : illegal use of local type in template
    instantiation

    Rearranging the program to:


    #include <iostream>
    #include <string>
    #include <vector>
    #include <iterator>
    #include <algorithm>

    using namespace std;

    struct A
    {
    void operator()(int i)
    {
    cout << i << endl;
    }
    };

    int main(int argc, char* argv[])
    {

    vector<int> v(3);

    for_each( v.begin(), v.end(), A() );

    return 0;
    }

    compiles with two warnings:

    warning C4100: 'argv' : unreferenced formal parameter
    warning C4100: 'argc' : unreferenced formal parameter

    Because of these errors I never use a local structure or class, they can't
    be used for templates which include the stl containers.

    --
    Jim Langston
    Jim Langston, Feb 18, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. karim
    Replies:
    1
    Views:
    770
    George Ter-Saakov
    Jun 26, 2003
  2. =?Utf-8?B?WVNVVA==?=

    Access local port or Running local exe file

    =?Utf-8?B?WVNVVA==?=, Jan 14, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    515
    =?Utf-8?B?WVNVVA==?=
    Jan 14, 2006
  3. E11
    Replies:
    1
    Views:
    4,743
    Thomas Weidenfeller
    Oct 12, 2005
  4. Sullivan WxPyQtKinter
    Replies:
    10
    Views:
    661
    Antoon Pardon
    Nov 8, 2007
  5. jrwats
    Replies:
    1
    Views:
    890
    Anthony Williams
    Mar 1, 2010
Loading...

Share This Page