overloaded ops << and >>

Discussion in 'C++' started by jalkadir, Jun 30, 2005.

  1. jalkadir

    jalkadir Guest

    I have a class that overloads the inserter and the extractor operators,
    but there is a problem with the way I worte the methods; because the
    compiler does not like it at all.
    This is the snip of what the problem looks like:

    --- money.hpp
    friend std::eek:stream& std::eek:perator<<( std::eek:stream&, const jme::Money&
    ); friend std::istream& std::eek:perator>>( std::istream&,
    jme::Money& );

    --- money.cpp
    std::eek:stream&
    std::eek:perator<<( std::eek:stream& os, const jme::Money& obj ) {
    os << obj.getAmount();
    return os;
    }

    std::istream&
    std::eek:perator>>( std::istream& is, jme::Money& obj ) {
    is >> obj.amount;
    return is;
    }

    --- error
    In file included from money.cpp:2:
    money.hpp:67: error: `std::eek:stream& std::eek:perator<<(std::eek:stream&,
    const jme::Money&)' should have been declared inside `std'
    money.hpp:68: error: `std::istream& std::eek:perator>>(std::istream&,
    jme::Money&)' should have been declared inside `std'

    What am I doing wrong?
    Can anybody help?

    TIA
    jalkadir, Jun 30, 2005
    #1
    1. Advertising

  2. jalkadir wrote:
    > I have a class that overloads the inserter and the extractor operators,
    > but there is a problem with the way I worte the methods; because the
    > compiler does not like it at all.
    > This is the snip of what the problem looks like:
    >
    > --- money.hpp
    > friend std::eek:stream& std::eek:perator<<( std::eek:stream&, const jme::Money&


    Drop the 'std::' in front of the "operator" keyword. In all places.

    > ); [...]
    Victor Bazarov, Jun 30, 2005
    #2
    1. Advertising

  3. jalkadir

    jalkadir Guest

    Thanks for the help, I am very thankful for it.
    I did as you suggested, but after the changes I get another error
    message. Let me explain: this is what the new changes look like:
    std::eek:stream&
    operator<<( std::eek:stream& os, const jme::Money& obj ) {
    os << obj.getAmount();
    return os;
    }
    std::istream&
    operator>>( std::istream& is, jme::Money& obj ) {
    is >> obj.amount;
    return is;
    }

    and this is the error I am getting:
    In file included from main.cpp:2:
    money.hpp:67: error: `std::eek:stream& std::eek:perator<<(std::eek:stream&,
    const jme::Money&)' should have been declared inside `std'
    money.hpp:68: error: `std::istream& std::eek:perator>>(std::istream&,
    jme::Money&)' should have been declared inside `std'


    Again, thanks for the help!!
    jalkadir, Jun 30, 2005
    #3
  4. jalkadir wrote:
    > Thanks for the help, I am very thankful for it.
    > I did as you suggested, but after the changes I get another error
    > message. Let me explain: this is what the new changes look like:
    > std::eek:stream&
    > operator<<( std::eek:stream& os, const jme::Money& obj ) {
    > os << obj.getAmount();
    > return os;
    > }
    > std::istream&
    > operator>>( std::istream& is, jme::Money& obj ) {
    > is >> obj.amount;
    > return is;
    > }
    >
    > and this is the error I am getting:
    > In file included from main.cpp:2:
    > money.hpp:67: error: `std::eek:stream& std::eek:perator<<(std::eek:stream&,
    > const jme::Money&)' should have been declared inside `std'
    > money.hpp:68: error: `std::istream& std::eek:perator>>(std::istream&,
    > jme::Money&)' should have been declared inside `std'


    You say you're getting "another" error message. How are these two
    different from what you got before? I can't find any difference.

    Are you sure you replaced _all_ "std::eek:perator<<" with "operator<<"?

    V
    Victor Bazarov, Jun 30, 2005
    #4
  5. jalkadir

    jalkadir Guest

    Howly mowly!!
    Yes, I am giving you the wrong info. Sorry about that. :(
    OK, here is what the new error looks like:

    --- Error
    money.hpp: In function `std::istream& operator>>(std::istream&,
    jme::Money&)':
    money.hpp:22: error: `float jme::Money::amount' is protected
    money.cpp:85: error: within this context

    --- Source Code
    std::eek:stream&
    operator<<( std::eek:stream& os, const jme::Money& obj ) {
    return os << obj.getAmount();
    //return os;
    }
    std::istream&
    operator>>( std::istream& is, jme::Money& obj ) {
    is >> obj.amount;
    return is;
    }

    --- Header Code
    namespace jme{
    class Money{
    protected: float amount;
    ........
    public:
    friend std::eek:stream& operator<<( std::eek:stream&, const
    jme::Money& );
    friend std::istream& operator>>( std::istream&, jme::Money&
    );
    -------

    I always thouhgt that we could access proteted or private member
    variables from a fiend method, is this true?
    Thanks again.
    jalkadir, Jun 30, 2005
    #5
  6. jalkadir wrote:
    > Howly mowly!!
    > Yes, I am giving you the wrong info. Sorry about that. :(
    > OK, here is what the new error looks like:
    >
    > --- Error
    > money.hpp: In function `std::istream& operator>>(std::istream&,
    > jme::Money&)':
    > money.hpp:22: error: `float jme::Money::amount' is protected
    > money.cpp:85: error: within this context
    >
    > --- Source Code
    > std::eek:stream&
    > operator<<( std::eek:stream& os, const jme::Money& obj ) {
    > return os << obj.getAmount();
    > //return os;
    > }
    > std::istream&
    > operator>>( std::istream& is, jme::Money& obj ) {
    > is >> obj.amount;
    > return is;
    > }
    >
    > --- Header Code
    > namespace jme{
    > class Money{
    > protected: float amount;
    > ........
    > public:
    > friend std::eek:stream& operator<<( std::eek:stream&, const
    > jme::Money& );
    > friend std::istream& operator>>( std::istream&, jme::Money&
    > );
    > -------
    >
    > I always thouhgt that we could access proteted or private member
    > variables from a fiend method, is this true?


    Yes, this is true. However, it seems that your compiler doesn't want to
    pick up the 'friend' declaration. Try changing the 'friend' declarations
    to

    friend std::eek:stream& ::eek:perator<<( .. );
    friend std::istream& ::eek:perator>>( .. );

    (notice the '::' in front of the 'operator' keyword).

    And if you declare those functions before the class definition, it would
    not hurt.

    V
    Victor Bazarov, Jun 30, 2005
    #6
  7. jalkadir

    jalkadir Guest

    That would tell the compiler that the 'operator' I am talking about
    would be the one belonging to the 'std', right? Which means that my
    previous statement (std::eek:perator<<) is the same as ::eek:perator<<(...),
    yes?

    Let me see what happens, back in a bit!
    jalkadir, Jun 30, 2005
    #7
  8. jalkadir wrote:
    > That would tell the compiler that the 'operator' I am talking about
    > would be the one belonging to the 'std', right? Which means that my
    > previous statement (std::eek:perator<<) is the same as ::eek:perator<<(...),
    > yes?


    No.

    > Let me see what happens, back in a bit!


    K. TTYL

    V
    Victor Bazarov, Jun 30, 2005
    #8
  9. jalkadir

    jalkadir Guest

    I am sorry Victor, I thought it ment the same thing. What does it
    mean? if you don't mind my question.

    On another front, here is the latest incarnation of my inserters and
    extractors:

    friend std::eek:stream& operator<<( std::eek:stream&, const jme::Money& );
    friend std::istream& operator>>( std::istream&, jme::Money& );


    std::eek:stream&
    operator<<( std::eek:stream& os, const jme::Money& obj ) {
    std::cout << "Check this out" << std::endl;
    return os << obj.getAmount();
    //return os;
    }
    std::istream&
    operator>>( std::istream& is, jme::Money& obj ) {
    float tmp;
    is >> tmp;
    std::cout << LINE << " " << FILE << " " << tmp << std::endl;
    obj.setAmount(tmp);
    return is;
    }

    I would expect that the statement

    jme::Money money("12.21");
    std::cout << " Value is: " << money << std::endl;

    would display 12.21, but in instead it displays 0x4c1094 !?

    I am really going *loco* with this part of the program. It just does
    not make sence.

    Can you help?
    jalkadir, Jul 1, 2005
    #9
  10. jalkadir wrote:
    > I am sorry Victor, I thought it ment the same thing. What does it
    > mean? if you don't mind my question.


    The absence of any scope resolution operator in front of the name
    means probably "the nearest enclosing namespace", in your case 'jme'.
    If you put '::' without anything before it, it will mean "global
    namespace". What book are you reading that doesn't mention the
    global namespace?


    > On another front, here is the latest incarnation of my inserters and
    > extractors:
    >
    > friend std::eek:stream& operator<<( std::eek:stream&, const jme::Money& );
    > friend std::istream& operator>>( std::istream&, jme::Money& );
    >
    >
    > std::eek:stream&
    > operator<<( std::eek:stream& os, const jme::Money& obj ) {
    > std::cout << "Check this out" << std::endl;
    > return os << obj.getAmount();
    > //return os;
    > }
    > std::istream&
    > operator>>( std::istream& is, jme::Money& obj ) {
    > float tmp;
    > is >> tmp;
    > std::cout << LINE << " " << FILE << " " << tmp << std::endl;
    > obj.setAmount(tmp);
    > return is;
    > }
    >
    > I would expect that the statement
    >
    > jme::Money money("12.21");
    > std::cout << " Value is: " << money << std::endl;
    >
    > would display 12.21, but in instead it displays 0x4c1094 !?
    >
    > I am really going *loco* with this part of the program. It just does
    > not make sence.


    Well, it seems that you have a conversion operator defined in your
    'jme::Money' class, which probably gives you 'void*' which the <<
    operator outputs as a number.

    Eliminate all extraneous things, make your program as simple as you
    can, and post it _complete_. With all the bits and pieces of the
    changing program I cannot make heads or tails of it.

    V
    Victor Bazarov, Jul 2, 2005
    #10
  11. jalkadir

    Old Wolf Guest

    jalkadir wrote:
    > --- Error
    > money.hpp: In function `std::istream& operator>>(std::istream&,
    > jme::Money&)':
    > money.hpp:22: error: `float jme::Money::amount' is protected
    > money.cpp:85: error: within this context
    >
    > std::eek:stream&
    > operator<<( std::eek:stream& os, const jme::Money& obj ) {
    > return os << obj.getAmount();
    > }
    > std::istream&
    > operator>>( std::istream& is, jme::Money& obj ) {
    > is >> obj.amount;
    > return is;
    > }


    These functions are in the global namespace.
    But they should be in the jme namespace.
    Because of argument-dependent lookup (introduced in the C++
    standard), a statement like:

    is >> obj;

    will only search for operator>> in the namespaces of 'is'
    and 'obj' (ie. 'std' and 'jme'), and NOT in the global namespace.

    Your declarations must look like this:

    namespace jme
    {
    std::eek:stream&
    operator<<( std::eek:stream& os, const jme::Money& obj );

    std::istream&
    operator>>( std::istream& is, jme::Money& obj );
    };

    The definitions can either look like that, or like:

    std::eek:stream& jme::eek:perator<<
    ( std::eek:stream& os, const jme::Money& obj )
    {
    return os << obj.getAmount();
    }

    >
    > --- Header Code
    > namespace jme{
    > class Money{
    > protected: float amount;
    > ........
    > public:
    > friend std::eek:stream& operator<<( std::eek:stream&, const
    > jme::Money& );
    > friend std::istream& operator>>( std::istream&, jme::Money&
    > );


    I'm not an expert on 'friend', but I think you need to declare
    these operator<< and operator>> BEFORE the friend declarations.

    Also, some compilers (eg. MSVC 6.0) have bugs and won't let you
    do this, even if you get it right.
    Old Wolf, Jul 4, 2005
    #11
    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. Wendy S
    Replies:
    3
    Views:
    3,151
    Blah Blah
    Jul 13, 2003
  2. Glen Able
    Replies:
    4
    Views:
    434
    Attila Feher
    Jan 28, 2004
  3. Replies:
    3
    Views:
    328
  4. mkr
    Replies:
    5
    Views:
    744
    pankaj.goel
    Nov 24, 2008
  5. gwowen
    Replies:
    3
    Views:
    307
Loading...

Share This Page