question about local class in a function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nan Li

    #1

    question about local class in a function

    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<intv(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::__no rmal_iterator<i nt*,
    std::vector<int , std::allocator< int >, main(int, char**)::A)'




  • Ian Collins

    #2
    Re: question about local class in a function

    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<intv(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::__no rmal_iterator<i nt*,
    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.

    Comment

    • Salt_Peter

      #3
      Re: question about local class in a function

      On Feb 17, 2:55 am, Nan Li <nan.l...@gmail .comwrote:
      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<intv(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::__no rmal_iterator<i nt*,
      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
      */


      Comment

      • Jerry Coffin

        #4
        Re: question about local class in a function

        In article <33e8504b-bca1-411f-bf19-b619a4846c67
        @q70g2000hsb.go oglegroups.com> , nan.li.g@gmail. 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<intv(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.beg in(), v.end(),
        std::ostream_it erator<int>(std ::cout, "\n"));

        --
        Later,
        Jerry.

        The universe is a figment of its own imagination.

        Comment

        Working...