Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > overloaded ops << and >>

Reply
Thread Tools

overloaded ops << and >>

 
 
jalkadir
Guest
Posts: n/a
 
      06-30-2005

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:stream& std:perator<<( std:stream&, const jme::Money&
); friend std::istream& std:perator>>( std::istream&,
jme::Money& );

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

std::istream&
std: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:stream& std:perator<<(std:stream&,
const jme::Money&)' should have been declared inside `std'
money.hpp:68: error: `std::istream& std:perator>>(std::istream&,
jme::Money&)' should have been declared inside `std'

What am I doing wrong?
Can anybody help?

TIA

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-30-2005
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:stream& std:perator<<( std:stream&, const jme::Money&


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

> ); [...]

 
Reply With Quote
 
 
 
 
jalkadir
Guest
Posts: n/a
 
      06-30-2005
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:stream&
operator<<( std: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:stream& std:perator<<(std:stream&,
const jme::Money&)' should have been declared inside `std'
money.hpp:68: error: `std::istream& std:perator>>(std::istream&,
jme::Money&)' should have been declared inside `std'


Again, thanks for the help!!

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      06-30-2005
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:stream&
> operator<<( std: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:stream& std:perator<<(std:stream&,
> const jme::Money&)' should have been declared inside `std'
> money.hpp:68: error: `std::istream& std: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:perator<<" with "operator<<"?

V
 
Reply With Quote
 
jalkadir
Guest
Posts: n/a
 
      06-30-2005
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:stream&
operator<<( std: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:stream& operator<<( std: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.

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      06-30-2005
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:stream&
> operator<<( std: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:stream& operator<<( std: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:stream& :perator<<( .. );
friend std::istream& :perator>>( .. );

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

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

V
 
Reply With Quote
 
jalkadir
Guest
Posts: n/a
 
      06-30-2005
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:perator<<) is the same as :perator<<(...),
yes?

Let me see what happens, back in a bit!

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      06-30-2005
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:perator<<) is the same as :perator<<(...),
> yes?


No.

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


K. TTYL

V
 
Reply With Quote
 
jalkadir
Guest
Posts: n/a
 
      07-01-2005
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:stream& operator<<( std:stream&, const jme::Money& );
friend std::istream& operator>>( std::istream&, jme::Money& );


std:stream&
operator<<( std: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?

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-02-2005
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:stream& operator<<( std:stream&, const jme::Money& );
> friend std::istream& operator>>( std::istream&, jme::Money& );
>
>
> std:stream&
> operator<<( std: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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Integer ops, associativity and precedence... gwowen C Programming 3 12-16-2009 11:43 PM
data types and arithmetic ops mkr VHDL 5 11-24-2008 06:19 AM
overload resolution and conversion ops xtrigger303@gmail.com C++ 3 05-19-2008 01:51 PM
Question about macro ops and the Lumix DMC-FZ7 bchernick@gmail.com Digital Photography 8 06-26-2006 06:12 AM
Simple questions on shift ops and promotions Glen Able C++ 4 01-28-2004 12:51 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57