overloaded inserters and extractors

J

jalkadir

I my program I have overloaded the inserters and extractor operator.
---name.hpp
friend std::eek:stream& std::eek:perator<<( std::eek:stream& os,
const jme::Name& obj );
friend std::istream& std::eek:perator>>( std::istream& is,
jme::Name& obj );
---name.cpp
std::eek:stream& std::eek:perator<<( std::eek:stream& os, const jme::Name& obj )
{
return os << obj.str;
}
std::istream& std::eek:perator>>( std::istream& is, jme::Name& obj ) {
return is >> obj.str;
}
------------------------
But this gives me an error that reads
Project : Name
Compiler : GNU GCC Compiler (called directly)
Directory : ..\dev\c++\jme\name\
--------------------------------------------------------------------------------
Switching to target: default
Compiling: main.cpp
In file included from main.cpp:3:
name.hpp:83: error: `std::eek:stream& std::eek:perator<<(std::eek:stream&, const
jme::Name&)' should have been declared inside `std'
name.hpp:85: error: `std::istream& std::eek:perator>>(std::istream&,
jme::Name&)' should have been declared inside `std'
Process terminated with status 1 (0 minutes, 5 seconds)

---------------------


What am I doing wrong?

TIA
 
V

Victor Bazarov

jalkadir said:
I my program I have overloaded the inserters and extractor operator.
---name.hpp
friend std::eek:stream& std::eek:perator<<( std::eek:stream& os,
^^^^^
You're calling this function 'operator<<' and declaring it a friend of
(apparently) the 'jme::Name' class, but allege that it is in the 'std'
namespace. Is it? In order to claim a member of 'std' as a friend, you
need to declare it *there* first.
const jme::Name& obj );
friend std::istream& std::eek:perator>>( std::istream& is,
jme::Name& obj );
---name.cpp
std::eek:stream& std::eek:perator<<( std::eek:stream& os, const jme::Name& obj )
{
return os << obj.str;
}
std::istream& std::eek:perator>>( std::istream& is, jme::Name& obj ) {
return is >> obj.str;
}
------------------------
But this gives me an error that reads
Project : Name
Compiler : GNU GCC Compiler (called directly)
Directory : ..\dev\c++\jme\name\
--------------------------------------------------------------------------------
Switching to target: default
Compiling: main.cpp
In file included from main.cpp:3:
name.hpp:83: error: `std::eek:stream& std::eek:perator<<(std::eek:stream&, const
jme::Name&)' should have been declared inside `std'
name.hpp:85: error: `std::istream& std::eek:perator>>(std::istream&,
jme::Name&)' should have been declared inside `std'
Process terminated with status 1 (0 minutes, 5 seconds)

Do you really need it in 'std'? I think any decent book on C++ covers the
subject of overloading stream insertion and extraction for a custom type.
What book are you reading?

V
 
J

jalkadir

This program outputs this:

"0x3d3cf8"
End of name...

but that is wrong, it should be,

"Niña"
End of name...

here is the progra snips.

--------- strtools.hpp
namespace{
calss strtools{
std::string str;
........

};
}
--------- name.hpp
namespace{
class Name : public jme::strtools{
....
// This only gives you an idea as to what the f'tions do
const std::string& getNameStr() const{return str;}
void setName( const std::string& x){str = x;}
void setName( const char* x){str = x;}

friend std::eek:stream& operator<<( std::eek:stream& os,
const jme::Name& obj );
friend std::istream& operator>>( std::istream& is,
jme::Name& obj );
};
}
--------- name.cpp
std::eek:stream& operator<<( std::eek:stream& os, const jme::Name& obj ) {
return os << obj.getNameStr(); }
std::istream& operator>>( std::istream& is, jme::Name& obj ) {
std::string local_tmp;
is >> local_tmp;
obj.setName(local_tmp);
return is;
}

--------- main.cpp
jme::Name* name;
try { name = new jme::Name("ni\xa4" "a" );}// niña
catch(std::bad_alloc& x) { std::cout << x.what() << std::endl; }
std::cout << "\"" << name << "\"" << std::endl;

delete name;
std::cout << "End of name..." << std::endl;
std::cin.get();
return 0;
}
========================================

what am I doing wrong?
Why am I no getting the right output?

TIA
 
I

int2str

jalkadir said:
This program outputs this:

"0x3d3cf8"
End of name...
Because:

jme::Name* name;
try { name = new jme::Name("ni\xa4" "a" );}// niña
catch(std::bad_alloc& x) { std::cout << x.what() << std::endl; }
std::cout << "\"" << name << "\"" << std::endl;

name is a pointer. What you get is the address of "name".
Try:
std::cout << "\"" << *name << "\"" << std::endl;

Cheers,
Andre
 
T

Thomas Maeder

[Why this Followup-To: header???]

jalkadir said:
I my program I have overloaded the inserters and extractor operator.
---name.hpp
friend std::eek:stream& std::eek:perator<<( std::eek:stream& os,
const jme::Name& obj );
friend std::istream& std::eek:perator>>( std::istream& is,
jme::Name& obj );

You can't do that.

The namespace std is sealed; the set of things we mere users are
allowed to do in it is restricted. Adding an operator overload is not
one of the things we are allowed to do.

Make these operators members of namespace jme.


Apart from that: What are these operators friend of?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top