Error with Function object..

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Senthilvel Samatharman

    Error with Function object..

    Hi,
    I'm trying to learn a few Algorithms and function objects
    But when i try the following program i get an error which i could not
    understand.
    Can you please help...

    Regards,
    Senthil.


    #include <iostream>
    #include <functional>
    #include <vector>

    using namespace std;

    template <class T> class Print
    {
    public:
    ostream outStream;
    public:
    Print(ostream ostr):outStream (ostr) { }
    void operator ( ) (const T& val)
    {
    outStream<<val< <endl;
    }
    };

    int main()
    {
    vector<int> arr;
    arr.push_back(1 );
    arr.push_back(2 );
    arr.push_back(3 );
    arr.push_back(4 );
    arr.push_back(5 );
    arr.push_back(6 );
    arr.push_back(7 );

    for_each(arr.be gin(),arr.end() ,Print<int>(cou t)); // << Error occurs
    here

    return 0;
    }

    But while compiling i get an error

    for_each.cpp: In method `ostream::ostre am(const ostream &)':
    /ap/gcc/gcc-bala/gcc-2.95.3/lib/gcc-lib/sparc-sun-solaris2.6/2.95.3/../../../../include/g++-3/streambuf.h:128 :
    `ios::ios(const ios &)' is private
    for_each.cpp:30 : within this context
    for_each.cpp: In function `int main()':
    for_each.cpp:30 : implicit declaration of function `int for_each(...)'
    for_each.cpp:30 : warning: cannot pass objects of type `Print<int>'
    through `...'

  • Gianni Mariani

    #2
    Re: Error with Function object..

    Senthilvel Samatharman wrote:[color=blue]
    > Hi,
    > I'm trying to learn a few Algorithms and function objects
    > But when i try the following program i get an error which i could not
    > understand.
    > Can you please help...
    >
    > Regards,
    > Senthil.[/color]

    ..... sample code removed
    [color=blue]
    > But while compiling i get an error
    >
    > for_each.cpp: In method `ostream::ostre am(const ostream &)':
    > /ap/gcc/gcc-bala/gcc-2.95.3/lib/gcc-lib/sparc-sun-solaris2.6/2.95.3/../../../../include/g++-3/streambuf.h:128 :
    > `ios::ios(const ios &)' is private
    > for_each.cpp:30 : within this context
    > for_each.cpp: In function `int main()':
    > for_each.cpp:30 : implicit declaration of function `int for_each(...)'
    > for_each.cpp:30 : warning: cannot pass objects of type `Print<int>'
    > through `...'
    >[/color]

    Here is better formatting ...

    #include <iostream>
    #include <functional>
    #include <vector>

    using namespace std;

    template <class T>
    class Print
    {
    public:
    ostream & outStream;
    ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^
    You can't copy outStream so we make it a reference

    public:
    Print(ostream & ostr):outStream (ostr) { }
    ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^
    Similarly here.


    void operator ( ) (const T& val)
    {
    outStream<<val< <endl;
    }
    };

    int main()
    {
    vector<int> arr;
    arr.push_back(1 );
    arr.push_back(2 );
    arr.push_back(3 );
    arr.push_back(4 );
    arr.push_back(5 );
    arr.push_back(6 );
    arr.push_back(7 );

    for_each(arr.be gin(),arr.end() ,Print<int>(cou t));
    return 0;
    }


    This compled and ran with gcc 3.3.1

    Comment

    • Noah Roberts

      #3
      Re: Error with Function object..

      Senthilvel Samatharman wrote:[color=blue]
      > Hi,
      > I'm trying to learn a few Algorithms and function objects
      > But when i try the following program i get an error which i could not
      > understand.
      > Can you please help...
      >
      > Regards,
      > Senthil.
      >
      >
      > #include <iostream>
      > #include <functional>
      > #include <vector>
      >
      > using namespace std;
      >
      > template <class T> class Print
      > {
      > public:
      > ostream outStream;
      > public:
      > Print(ostream ostr):outStream (ostr) { }
      > void operator ( ) (const T& val)
      > {
      > outStream<<val< <endl;
      > }
      > };
      >
      > int main()
      > {
      > vector<int> arr;
      > arr.push_back(1 );
      > arr.push_back(2 );
      > arr.push_back(3 );
      > arr.push_back(4 );
      > arr.push_back(5 );
      > arr.push_back(6 );
      > arr.push_back(7 );
      >
      > for_each(arr.be gin(),arr.end() ,Print<int>(cou t)); // << Error occurs
      > here
      >
      > return 0;
      > }
      >
      > But while compiling i get an error
      >
      > for_each.cpp: In method `ostream::ostre am(const ostream &)':
      > /ap/gcc/gcc-bala/gcc-2.95.3/lib/gcc-lib/sparc-sun-solaris2.6/2.95.3/../../../../include/g++-3/streambuf.h:128 :
      > `ios::ios(const ios &)' is private
      > for_each.cpp:30 : within this context
      > for_each.cpp: In function `int main()':
      > for_each.cpp:30 : implicit declaration of function `int for_each(...)'[/color]

      for_each is declared in <algorithm> I believe.
      [color=blue]
      > for_each.cpp:30 : warning: cannot pass objects of type `Print<int>'
      > through `...'
      >[/color]


      Comment

      • Dave Theese

        #4
        Re: Error with Function object..

        For one thing, you need to #include <algorithm> for for_each() unless it's
        somehow getting indirectly included. Not sure if this will fix everything,
        but it's a good place to start!

        "Senthilvel Samatharman" <Samatharman.Se nthilvel@adcc.a lcatel.be> wrote in
        message news:3F5A085B.E E105D7F@adcc.al catel.be...[color=blue]
        > Hi,
        > I'm trying to learn a few Algorithms and function objects
        > But when i try the following program i get an error which i could not
        > understand.
        > Can you please help...
        >
        > Regards,
        > Senthil.
        >
        >
        > #include <iostream>
        > #include <functional>
        > #include <vector>
        >
        > using namespace std;
        >
        > template <class T> class Print
        > {
        > public:
        > ostream outStream;
        > public:
        > Print(ostream ostr):outStream (ostr) { }
        > void operator ( ) (const T& val)
        > {
        > outStream<<val< <endl;
        > }
        > };
        >
        > int main()
        > {
        > vector<int> arr;
        > arr.push_back(1 );
        > arr.push_back(2 );
        > arr.push_back(3 );
        > arr.push_back(4 );
        > arr.push_back(5 );
        > arr.push_back(6 );
        > arr.push_back(7 );
        >
        > for_each(arr.be gin(),arr.end() ,Print<int>(cou t)); // << Error occurs
        > here
        >
        > return 0;
        > }
        >
        > But while compiling i get an error
        >
        > for_each.cpp: In method `ostream::ostre am(const ostream &)':
        >[/color]
        /ap/gcc/gcc-bala/gcc-2.95.3/lib/gcc-lib/sparc-sun-solaris2.6/2.95.3/../../..
        /../include/g++-3/streambuf.h:128 :[color=blue]
        > `ios::ios(const ios &)' is private
        > for_each.cpp:30 : within this context
        > for_each.cpp: In function `int main()':
        > for_each.cpp:30 : implicit declaration of function `int for_each(...)'
        > for_each.cpp:30 : warning: cannot pass objects of type `Print<int>'
        > through `...'
        >[/color]


        Comment

        • Kevin Goodsell

          #5
          Re: Error with Function object..

          Dave Theese wrote:
          [color=blue]
          > For one thing[/color]
          <snip>

          Please don't top-post. Re-read section 5 of the FAQ for posting
          guidelines. You could also find this rule in pretty much any netiquette
          reference, including RFC 1855.



          -Kevin
          --
          My email address is valid, but changes periodically.
          To contact me please use the address from a recent posting.

          Comment

          Working...