Thread: [Dev-C++] Getting the wrong output
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <jal...@go...> - 2005-06-30 12:32:45
|
In my class, when I use the "getter" to display the value I get the right
output, but when I try using the overloaded extractor operator (<<) I get
some hex value displayed. Here is some of the code:
--- snip
Main.cpp
int main() {
Money money("12.21");
std::cout << "Value is: " << money.getAmount() << std::endl; // 12.21
std::cout << "Value is: " << money << std::endl; // hex value????
return 0;
}
The class goes like this:
---snip
Money.hpp
class Money {
protected:
float amount; //!< This variable holds the numerical value
public:
//! Constor
Money(const std::string&);
//!Copy constructor
Money( const Money& );
//!Destructor
~Money() { std::cout << amount << std::endl;} //<<=3D=3D=3D 12.21
// Setters
...
// Getters
const float getAmount() const;
// Overloaded operators
.....
friend std::ostream& operator<<( std::ostream&, const jme::Money& );
friend std::istream& operator>>( std::istream&, jme::Money& );
....
} ; //Money
and the code looks like this
--- snip
jme::Money::Money( const std::string& x){
this->setAmount(x); // this metho converts the std::string to a float
}
const float jme::Money::getAmount() const {
return this->amount;
}
std::ostream&
jme::operator<<( std::ostream& os, const jme::Money& obj ) {
os << obj.getAmount(); //<<=3D=3D=3D=3D 0x4b1120
return os;
}
----- end of snip
As you can see when using the member method getAmount() the value
displayed is in the right format, however the extractor operator is not
doing the job I thought it would.
Can anybody tell me what I am doing wrong?
TIA
|
|
From: Per W. <pw...@ia...> - 2005-06-30 13:26:05
|
What namespaces are you using.
You have declared the friend operator as:
std::ostream& operator<<(...)
and implemented it as:
std::ostream& jme::operator<<(...)
Without a maching output operator, the compiler will emit the pointer to
the object instead.
/Per W
On Thu, 30 Jun 2005 jal...@go... wrote:
> In my class, when I use the "getter" to display the value I get the right
> output, but when I try using the overloaded extractor operator (<<) I get
> some hex value displayed. Here is some of the code:
> --- snip
> Main.cpp
> int main() {
> Money money("12.21");
>
> std::cout << "Value is: " << money.getAmount() << std::endl; // 12.21
> std::cout << "Value is: " << money << std::endl; // hex value????
> return 0;
> }
>
>
>
> The class goes like this:
> ---snip
> Money.hpp
> class Money {
> protected:
> float amount; //!< This variable holds the numerical value
> public:
> //! Constor
> Money(const std::string&);
>
> //!Copy constructor
> Money( const Money& );
>
> //!Destructor
> ~Money() { std::cout << amount << std::endl;} //<<=== 12.21
>
> // Setters
> ...
> // Getters
> const float getAmount() const;
>
> // Overloaded operators
> .....
> friend std::ostream& operator<<( std::ostream&, const jme::Money& );
> friend std::istream& operator>>( std::istream&, jme::Money& );
> ....
> } ; //Money
>
>
>
> and the code looks like this
> --- snip
> jme::Money::Money( const std::string& x){
> this->setAmount(x); // this metho converts the std::string to a float
> }
> const float jme::Money::getAmount() const {
> return this->amount;
> }
> std::ostream&
> jme::operator<<( std::ostream& os, const jme::Money& obj ) {
> os << obj.getAmount(); //<<==== 0x4b1120
> return os;
> }
> ----- end of snip
> As you can see when using the member method getAmount() the value
> displayed is in the right format, however the extractor operator is not
> doing
|
|
From: <jal...@go...> - 2005-06-30 15:09:22
|
Yes, I just notice that, so this are the changes I have made:
--- Money.hpp
friend std::ostream& std::operator<<( std::ostream&, const jme::Money& );
friend std::istream& std::operator>>( std::istream&, jme::Money& );
--- Money.cpp
std::operator<<( std::ostream& os, const jme::Money& obj ) {
os << obj.getAmount();
return os;
}
std::istream&
std::operator>>( std::istream& is, jme::Money& obj ) {
is >> obj.amount;
return is;
}
--------------
But this is giving me linker errors sayng:
=3D=3D=3D=3D=3D=3D=3D=3D=3D ERROR =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
In file included from money.cpp:2:
money.hpp:67: error: `std::ostream& std::operator<<(std::ostream&, const
jme::Money&)' should have been declared inside `std'
money.hpp:68: error: `std::istream& std::operator>>(std::istream&,
jme::Money&)' should have been declared inside `std'
What am I doing wrong!!?
Again thanks for you help.
> What namespaces are you using.
>
> You have declared the friend operator as:
> std::ostream& operator<<(...)
> and implemented it as:
> std::ostream& jme::operator<<(...)
>
> Without a maching output operator, the compiler will emit the pointer t=
o
> the object instead.
>
> /Per W
>
> On Thu, 30 Jun 2005 jal...@go... wrote:
>
>> In my class, when I use the "getter" to display the value I get the
>> right
>> output, but when I try using the overloaded extractor operator (<<) I
>> get
>> some hex value displayed. Here is some of the code:
>> --- snip
>> Main.cpp
>> int main() {
>> Money money("12.21");
>>
>> std::cout << "Value is: " << money.getAmount() << std::endl; // 12.=
21
>> std::cout << "Value is: " << money << std::endl; // hex value????
>> return 0;
>> }
>>
>>
>>
>> The class goes like this:
>> ---snip
>> Money.hpp
>> class Money {
>> protected:
>> float amount; //!< This variable holds the numerical value
>> public:
>> //! Constor
>> Money(const std::string&);
>>
>> //!Copy constructor
>> Money( const Money& );
>>
>> //!Destructor
>> ~Money() { std::cout << amount << std::endl;} //<<=3D=3D=3D 12.21
>>
>> // Setters
>> ...
>> // Getters
>> const float getAmount() const;
>>
>> // Overloaded operators
>> .....
>> friend std::ostream& operator<<( std::ostream&, const jme::Money& )=
;
>> friend std::istream& operator>>( std::istream&, jme::Money& );
>> ....
>> } ; //Money
>>
>>
>>
>> and the code looks like this
>> --- snip
>> jme::Money::Money( const std::string& x){
>> this->setAmount(x); // this metho converts the std::string to a
>> float
>> }
>> const float jme::Money::getAmount() const {
>> return this->amount;
>> }
>> std::ostream&
>> jme::operator<<( std::ostream& os, const jme::Money& obj ) {
>> os << obj.getAmount(); //<<=3D=3D=3D=3D 0x4b1120
>> return os;
>> }
>> ----- end of snip
>> As you can see when using the member method getAmount() the value
>> displayed is in the right format, however the extractor operator is no=
t
>> doing
>
>
>
> -------------------------------------------------------
> SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
> from IBM. Find simple to follow Roadmaps, straightforward articles,
> informative Webcasts and more! Get everything you need to get up to
> speed, fast. http://ads.osdn.com/?ad_id=3D7477&alloc_id=3D16492&op=3Dcl=
ick
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Franziska E. <fra...@ya...> - 2005-06-30 16:09:32
|
hi...
you should remove the std:: before operator:
--- Money.hpp
friend ostream& std::operator<<( std::ostream&, const jme::Money& );
friend istream& std::operator>>( std::istream&, jme::Money& );
--- Money.cpp
operator<<( std::ostream& os, const jme::Money& obj ) {
os << obj.getAmount();
return os;
}
std::istream&
operator>>( std::istream& is, jme::Money& obj ) {
is >> obj.amount;
return is;
}
>Yes, I just notice that, so this are the changes I have made:
>
>--- Money.hpp
>friend std::ostream& std::operator<<( std::ostream&, const jme::Money& );
>friend std::istream& std::operator>>( std::istream&, jme::Money& );
>
>
>--- Money.cpp
>std::operator<<( std::ostream& os, const jme::Money& obj ) {
> os << obj.getAmount();
> return os;
>}
>std::istream&
>std::operator>>( std::istream& is, jme::Money& obj ) {
> is >> obj.amount;
> return is;
>}
>--------------
>But this is giving me linker errors sayng:
>
>
>========= ERROR ===========
>In file included from money.cpp:2:
>money.hpp:67: error: `std::ostream& std::operator<<(std::ostream&, const
>jme::Money&)' should have been declared inside `std'
>money.hpp:68: error: `std::istream& std::operator>>(std::istream&,
>jme::Money&)' should have been declared inside `std'
>
>
>What am I doing wrong!!?
>
>Again thanks for you help.
>
>
>
>
>
>
>>What namespaces are you using.
>>
>>You have declared the friend operator as:
>> std::ostream& operator<<(...)
>>and implemented it as:
>> std::ostream& jme::operator<<(...)
>>
>>Without a maching output operator, the compiler will emit the pointer to
>>the object instead.
>>
>>/Per W
>>
>>On Thu, 30 Jun 2005 jal...@go... wrote:
>>
>>
>>
>>>In my class, when I use the "getter" to display the value I get the
>>>right
>>>output, but when I try using the overloaded extractor operator (<<) I
>>>get
>>>some hex value displayed. Here is some of the code:
>>>--- snip
>>>Main.cpp
>>>int main() {
>>> Money money("12.21");
>>>
>>> std::cout << "Value is: " << money.getAmount() << std::endl; // 12.21
>>> std::cout << "Value is: " << money << std::endl; // hex value????
>>> return 0;
>>>}
>>>
>>>
>>>
>>>The class goes like this:
>>>---snip
>>>Money.hpp
>>>class Money {
>>> protected:
>>> float amount; //!< This variable holds the numerical value
>>> public:
>>> //! Constor
>>> Money(const std::string&);
>>>
>>> //!Copy constructor
>>> Money( const Money& );
>>>
>>> //!Destructor
>>> ~Money() { std::cout << amount << std::endl;} //<<=== 12.21
>>>
>>> // Setters
>>> ...
>>> // Getters
>>> const float getAmount() const;
>>>
>>> // Overloaded operators
>>> .....
>>> friend std::ostream& operator<<( std::ostream&, const jme::Money& );
>>> friend std::istream& operator>>( std::istream&, jme::Money& );
>>> ....
>>>} ; //Money
>>>
>>>
>>>
>>>and the code looks like this
>>>--- snip
>>>jme::Money::Money( const std::string& x){
>>> this->setAmount(x); // this metho converts the std::string to a
>>>float
>>>}
>>>const float jme::Money::getAmount() const {
>>> return this->amount;
>>>}
>>>std::ostream&
>>>jme::operator<<( std::ostream& os, const jme::Money& obj ) {
>>> os << obj.getAmount(); //<<==== 0x4b1120
>>> return os;
>>>}
>>>----- end of snip
>>>As you can see when using the member method getAmount() the value
>>>displayed is in the right format, however the extractor operator is not
>>>doing
>>>
>>>
>>
>>-------------------------------------------------------
>>SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
>>from IBM. Find simple to follow Roadmaps, straightforward articles,
>>informative Webcasts and more! Get everything you need to get up to
>>speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
>>_______________________________________________
>>Dev-cpp-users mailing list
>>Dev...@li...
>>TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
>>https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>>
>>
>>
>
>
>
>-------------------------------------------------------
>SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
>from IBM. Find simple to follow Roadmaps, straightforward articles,
>informative Webcasts and more! Get everything you need to get up to
>speed, fast. http://ads.osdn.com/?ad_idt77&alloc_id492&op=click
>_______________________________________________
>Dev-cpp-users mailing list
>Dev...@li...
>TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
>https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
>
>
>
___________________________________________________________
Gesendet von Yahoo! Mail - Jetzt mit 1GB Speicher kostenlos - Hier anmelden: http://mail.yahoo.de
|
|
From: Per W. <pw...@ia...> - 2005-06-30 16:54:48
|
std::xx is for symbols declared inside of the std namespace. Your own
operator is definitely not defined in the std namespace - and you should
never try to define anyting in that namespace. It is only used for
standardized variables, types, methods etc.
You only add the xx:: prefix to a symbol when referencing symbols outside
of the current namespace or any namespace you have specifically said you
are using.
If you don't use any namespaces yourself, just write:
friend std::ostream& operator<<(std::ostream&,const jme::Money&);
std::ostream& operator<<(std::ostream& os,const jme::Money& obj) {
...
}
If the operator was defined within the jme namespace, then you have to
also do the friend declaration specifying:
friend std::ostream& jme::operator<<(std::ostream&,const jme::Money&);
and somewhere else specify the implementation within the jme namespace:
:
namespace jme {
std::ostream& operator<<(std::ostream& os,const Money&) {
...
}
}
/Per W
On Thu, 30 Jun 2005 jal...@go... wrote:
> Yes, I just notice that, so this are the changes I have made:
>
> --- Money.hpp
> friend std::ostream& std::operator<<( std::ostream&, const jme::Money& );
> friend std::istream& std::operator>>( std::istream&, jme::Money& );
>
>
> --- Money.cpp
> std::operator<<( std::ostream& os, const jme::Money& obj ) {
> os << obj.getAmount();
> return os;
> }
> std::istream&
> std::operator>>( std::istream& is, jme::Money& obj ) {
> is >> obj.amount;
> return is;
> }
> --------------
> But this is giving me linker errors sayng:
>
>
> ========= ERROR ===========
> In file included from money.cpp:2:
> money.hpp:67: error: `std::ostream& std::operator<<(std::ostream&, const
> jme::Money&)' should have been declared inside `std'
> money.hpp:68: error: `std::istream& std::operator>>(std::istream&,
> jme::Money&)' should have been declared inside `std'
>
>
> What am I doing wrong!!?
>
> Again thanks for you help.
>
>
>
>
> > What namespaces are you using.
> >
> > You have declared the friend operator as:
> > std::ostream& operator<<(...)
> > and implemented it as:
> > std::ostream& jme::operator<<(...)
> >
> > Without a maching output operator, the compiler will emit the pointer to
> > the object instead.
> >
> > /Per W
> >
> > On Thu, 30 Jun 2005 jal...@go... wrote:
> >
> >> In my class, when I use the "getter" to display the value I get the
> >> right
> >> output, but when I try using the overloaded extractor operator (<<) I
> >> get
> >> some hex value displayed. Here is some of the code:
> >> --- snip
> >> Main.cpp
> >> int main() {
> >> Money money("12.21");
> >>
> >> std::cout << "Value is: " << money.getAmount() << std::endl; // 12.21
> >> std::cout << "Value is: " << money << std::endl; // hex value????
> >> return 0;
> >> }
> >>
> >>
> >>
> >> The class goes like this:
> >> ---snip
> >> Money.hpp
> >> class Money {
> >> protected:
> >> float amount; //!< This variable holds the numerical value
> >> public:
> >> //! Constor
> >> Money(const std::string&);
> >>
> >> //!Copy constructor
> >> Money( const Money& );
> >>
> >> //!Destructor
> >> ~Money() { std::cout << amount << std::endl;} //<<=== 12.21
> >>
> >> // Setters
> >> ...
> >> // Getters
> >> const float getAmount() const;
> >>
> >> // Overloaded operators
> >> .....
> >> friend std::ostream& operator<<( std::ostream&, const jme::Money& );
> >> friend std::istream& operator>>( std::istream&, jme::Money& );
> >> ....
> >> } ; //Money
> >>
> >>
> >>
> >> and the code looks like this
> >> --- snip
> >> jme::Money::Money( const std::string& x){
> >> this->setAmount(x); // this metho converts the std::string to a
> >> float
> >> }
> >> const float jme::Money::getAmount() const {
> >> return this->amount;
> >> }
> >> std::ostream&
> >> jme::operator<<( std::ostream& os, const jme::Money& obj ) {
> >> os << obj.getAmount(); //<<==== 0x4b1120
> >> return os;
> >> }
> >> ----- end of snip
> >> As you can see when using the member method getAmount() the value
> >> displayed is in the right format, however the extractor operator is not
> >> doing
|