I my program I have overloaded the inserters and extractor operator.
---name.hpp
friend std::ostream& std::operator<<( std::ostream& os,
const jme::Name& obj );
friend std::istream& std::operator>>( std::istream& is,
jme::Name& obj );
---name.cpp
std::ostream& std::operator<<( std::ostream& os, const jme::Name& obj )
{
return os << obj.str;
}
std::istream& std::operator>>( 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::ostream& std::operator<<(std::ostream&, const
jme::Name&)' should have been declared inside `std'
name.hpp:85: error: `std::istream& std::operator>>(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 5 2058
jalkadir wrote: I my program I have overloaded the inserters and extractor operator. ---name.hpp friend std::ostream& std::operator<<( std::ostream& 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::operator>>( std::istream& is, jme::Name& obj ); ---name.cpp std::ostream& std::operator<<( std::ostream& os, const jme::Name& obj ) { return os << obj.str; } std::istream& std::operator>>( 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::ostream& std::operator<<(std::ostream&, const jme::Name&)' should have been declared inside `std' name.hpp:85: error: `std::istream& std::operator>>(std::istream&, jme::Name&)' should have been declared inside `std' Process terminated with status 1 (0 minutes, 5 seconds)
---------------------
What am I doing wrong?
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
Ah! thanks!
I must be getting overworked.
:)
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::ostream& operator<<( std::ostream& os,
const jme::Name& obj );
friend std::istream& operator>>( std::istream& is,
jme::Name& obj );
};
}
--------- name.cpp
std::ostream& operator<<( std::ostream& 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
jalkadir wrote: 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
[Why this Followup-To: header???]
"jalkadir" <ja******@gosonic.ca> writes: I my program I have overloaded the inserters and extractor operator. ---name.hpp friend std::ostream& std::operator<<( std::ostream& os, const jme::Name& obj ); friend std::istream& std::operator>>( 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? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Nitin Bhardwaj |
last post by:
Thanx in advance for the response...
I wanna enquire ( as it is asked many a times in Interviews that i
face as an Engg PostGraduate ) about the overloading capability of the
C++ Language.
...
|
by: masood.iqbal |
last post by:
I have a few questions regarding overloaded typecast
operators and copy constructors that I would like an
answer for. Thanks in advance.
Masood
(1) In some examples that I have seen...
|
by: masood.iqbal |
last post by:
Please help me with this doubt that I have regarding overloaded
operators. Sometimes they are member functions and sometimes they are
friends (e.g. see the code snippet from Stroustrup, Second...
|
by: bahadir.balban |
last post by:
Hi,
What's the best way to implement an overloaded function in C? For
instance if you want to have generic print function for various
structures, my implementation would be with a case...
|
by: cybertof |
last post by:
Hello,
Is it possible in C# to have 2 overloaded functions with
- same names
- same parameters
- different return type values
If no, is it possible in another language ?
|
by: B. Williams |
last post by:
I have an assignment for school to Overload the operators << and >and I
have written the code, but I have a problem with the insertion string
function. I can't get it to recognize the second of...
|
by: raylopez99 |
last post by:
I need an example of a managed overloaded assignment operator for a
reference class, so I can equate two classes A1 and A2, say called
ARefClass, in this manner: A1=A2;. For some strange reason...
|
by: desktop |
last post by:
If a function should work with different types you normally overload it:
void myfun(int a){
// do int stuff
}
void myfun(std::string str){
// do string stuff
}
|
by: subramanian100in |
last post by:
overloaded operator=()
--------------------------------
overloaded assignment operator should be a non-static MEMBER function
of a class. This ensures that the first operand is an lvalue.
If...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |