473,856 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem regarding overloading of operator <<.

hi guys,

i have overloaded the << operator.as shown below.
ostream& operator<<(ostr eam &out, const student &a)
{
out<<a.idno;
out<< " " ;
// out<< a.name;
out<< " " ;
// out<< a.marks << endl;
return out;
}
and
ostream& operator<<(ostr eam &out,string1 &s1)
{
char *p;
p=s1.c_str();
out<<p;
return out;
}
here string1 is the string class i have created and student is
class student
{
public:
string1 idno;
string1 name;
int marks;
};
now i am getting this error
no match for `std::basic_ost ream<char, std::char_trait s<char>
>& << const string1&' operator
at the line
out<<a.idno;
the overloaded operator << for string1 class is working for other
implementation.
kindly explain me this error.
thanking you in advance.

Jul 28 '06 #1
17 2361
Hi,
ostream& operator<<(ostr eam &out, const student &a)
You are accepting the reference as const .
ostream& operator<<(ostr eam &out,string1 &s1)
this is just reference.
out<<a.idno;
As "a" is const, this call is not working with overloaded operator
accepting non-const reference to string1.

Use this =ostream& operator<<(ostr eam &out,const string1 &s1)
It should work.

Regards,
Amir

Jul 28 '06 #2
Ashwin wrote:
hi guys,

i have overloaded the << operator.as shown below.
ostream& operator<<(ostr eam &out, const student &a)
{
out<<a.idno;
out<< " " ;
// out<< a.name;
out<< " " ;
// out<< a.marks << endl;
return out;
}
and
ostream& operator<<(ostr eam &out,string1 &s1)
This should read:

stream& operator<<(ostr eam &out,string1 const &s1)
{
char *p;
p=s1.c_str();
out<<p;
return out;
}
Is it intentional that your string class does not allow for embedded 0
characters within strings?
here string1 is the string class i have created
Why did you create your own string class? Usually, this is a BadIdea(tm),
and I am inclined to bet that your case is in that category, too.
and student is
class student
{
public:
string1 idno;
string1 name;
int marks;
};
now i am getting this error
no match for `std::basic_ost ream<char, std::char_trait s<char>
>& << const string1&' operator
at the line
out<<a.idno;
the overloaded operator << for string1 class is working for other
implementation.
Which other implementation?
ps.: just use std::string.
Best

Kai-Uwe Bux
Jul 28 '06 #3

am******@yahoo. com wrote:
Hi,
ostream& operator<<(ostr eam &out, const student &a)
You are accepting the reference as const .
ostream& operator<<(ostr eam &out,string1 &s1)
this is just reference.
out<<a.idno;
As "a" is const, this call is not working with overloaded operator
accepting non-const reference to string1.

Use this =ostream& operator<<(ostr eam &out,const string1 &s1)
It should work.

Regards,
Amir
thanks Amir.
now i understand what is the problem . your explanation was very nice.
thanks a lot

Jul 28 '06 #4

Kai-Uwe Bux wrote:
Ashwin wrote:
hi guys,

i have overloaded the << operator.as shown below.
ostream& operator<<(ostr eam &out, const student &a)
{
out<<a.idno;
out<< " " ;
// out<< a.name;
out<< " " ;
// out<< a.marks << endl;
return out;
}
and
ostream& operator<<(ostr eam &out,string1 &s1)

This should read:

stream& operator<<(ostr eam &out,string1 const &s1)
{
char *p;
p=s1.c_str();
out<<p;
return out;
}

Is it intentional that your string class does not allow for embedded 0
characters within strings?
here string1 is the string class i have created

Why did you create your own string class? Usually, this is a BadIdea(tm),
and I am inclined to bet that your case is in that category, too.
and student is
class student
{
public:
string1 idno;
string1 name;
int marks;
};
now i am getting this error
no match for `std::basic_ost ream<char, std::char_trait s<char>
>& << const string1&' operator
at the line
out<<a.idno;
the overloaded operator << for string1 class is working for other
implementation.

Which other implementation?
ps.: just use std::string.
Best

Kai-Uwe Bux
thanks Kai-Uwe Bux for replying . i wrote my own string class from a
linked list as it was given to me as an assignment now i have to make
use of this string class. other implementation means other .cpp files
in which i have used my string class.
thanx and regards
ashwin

Jul 28 '06 #5

Ashwin wrote:
Kai-Uwe Bux wrote:
Ashwin wrote:
hi guys,
>
i have overloaded the << operator.as shown below.
ostream& operator<<(ostr eam &out, const student &a)
{
out<<a.idno;
out<< " " ;
// out<< a.name;
out<< " " ;
// out<< a.marks << endl;
return out;
}
and
ostream& operator<<(ostr eam &out,string1 &s1)
This should read:

stream& operator<<(ostr eam &out,string1 const &s1)
{
char *p;
p=s1.c_str();
out<<p;
return out;
}
Is it intentional that your string class does not allow for embedded 0
characters within strings?
here string1 is the string class i have created
Why did you create your own string class? Usually, this is a BadIdea(tm),
and I am inclined to bet that your case is in that category, too.
and student is
class student
{
public:
string1 idno;
string1 name;
int marks;
};
now i am getting this error
no match for `std::basic_ost ream<char, std::char_trait s<char>
>& << const string1&' operator
at the line
out<<a.idno;
the overloaded operator << for string1 class is working for other
implementation.
Which other implementation?
ps.: just use std::string.
Best

Kai-Uwe Bux

thanks Kai-Uwe Bux for replying . i wrote my own string class from a
linked list as it was given to me as an assignment now i have to make
use of this string class. other implementation means other .cpp files
in which i have used my string class.
thanx and regards
ashwin

hi guys ,

sorry to disturb you again . can any one explain me this error
/usr/include/c++/3.2.2/bits/stl_construct.h : In function `void
std::_Construct (_T1*, const _T2&) [with _T1 = student, _T2 =
student]':
/usr/include/c++/3.2.2/bits/stl_list.h:328: instantiated from
`std::_List_nod e<_Tp>* std::list<_Tp, _Alloc>::_M_cre ate_node(const
_Tp&) [with _Tp = student, _Alloc = std::allocator< student>]'
/usr/include/c++/3.2.2/bits/stl_list.h:430: instantiated from
`std::_List_ite rator<_Tp, _Tp&, _Tp*std::list<_ Tp,
_Alloc>::insert (std::_List_ite rator<_Tp, _Tp&, _Tp*>, const _Tp&) [with
_Tp = student, _Alloc = std::allocator< student>]'
/usr/include/c++/3.2.2/bits/stl_list.h:479: instantiated from `void
std::list<_Tp, _Alloc>::push_b ack(const _Tp&) [with _Tp = student,
_Alloc = std::allocator< student>]'
assign2.cpp:100 : instantiated from here
/usr/include/c++/3.2.2/bits/stl_construct.h :78: no matching function
for call
to `student::stude nt(const student&)'
assign2.cpp:80: candidates are: student::studen t()
assign2.cpp:8: student::studen t(student&)

at assign2.cpp:100 l.push_back(s); is present

here l is list<studentl;
and s is an object of class student.

kindly explain me this error.

Jul 28 '06 #6
Ashwin wrote:
hi guys ,

sorry to disturb you again . can any one explain me this error
I thought you were going to fix your capitalisation?
/usr/include/c++/3.2.2/bits/stl_construct.h : In function `void
std::_Construct (_T1*, const _T2&) [with _T1 = student, _T2 =
student]':
/usr/include/c++/3.2.2/bits/stl_list.h:328: instantiated from
`std::_List_nod e<_Tp>* std::list<_Tp, _Alloc>::_M_cre ate_node(const
_Tp&) [with _Tp = student, _Alloc = std::allocator< student>]'
/usr/include/c++/3.2.2/bits/stl_list.h:430: instantiated from
`std::_List_ite rator<_Tp, _Tp&, _Tp*std::list<_ Tp,
_Alloc>::insert (std::_List_ite rator<_Tp, _Tp&, _Tp*>, const _Tp&) [with
_Tp = student, _Alloc = std::allocator< student>]'
/usr/include/c++/3.2.2/bits/stl_list.h:479: instantiated from `void
std::list<_Tp, _Alloc>::push_b ack(const _Tp&) [with _Tp = student,
_Alloc = std::allocator< student>]'
assign2.cpp:100 : instantiated from here
/usr/include/c++/3.2.2/bits/stl_construct.h :78: no matching function
for call
to `student::stude nt(const student&)'
assign2.cpp:80: candidates are: student::studen t()
assign2.cpp:8: student::studen t(student&)
The compiler is telling you that student doesn't have a copy
constructor. Any user defined type used with a standard container must
have a public copy constructor if any other constructor is declared
because the object is copied into the container.
--
Ian Collins.
Jul 28 '06 #7

Ian Collins wrote:
Ashwin wrote:
hi guys ,

sorry to disturb you again . can any one explain me this error

I thought you were going to fix your capitalisation?
/usr/include/c++/3.2.2/bits/stl_construct.h : In function `void
std::_Construct (_T1*, const _T2&) [with _T1 = student, _T2 =
student]':
/usr/include/c++/3.2.2/bits/stl_list.h:328: instantiated from
`std::_List_nod e<_Tp>* std::list<_Tp, _Alloc>::_M_cre ate_node(const
_Tp&) [with _Tp = student, _Alloc = std::allocator< student>]'
/usr/include/c++/3.2.2/bits/stl_list.h:430: instantiated from
`std::_List_ite rator<_Tp, _Tp&, _Tp*std::list<_ Tp,
_Alloc>::insert (std::_List_ite rator<_Tp, _Tp&, _Tp*>, const _Tp&) [with
_Tp = student, _Alloc = std::allocator< student>]'
/usr/include/c++/3.2.2/bits/stl_list.h:479: instantiated from `void
std::list<_Tp, _Alloc>::push_b ack(const _Tp&) [with _Tp = student,
_Alloc = std::allocator< student>]'
assign2.cpp:100 : instantiated from here
/usr/include/c++/3.2.2/bits/stl_construct.h :78: no matching function
for call
to `student::stude nt(const student&)'
assign2.cpp:80: candidates are: student::studen t()
assign2.cpp:8: student::studen t(student&)
The compiler is telling you that student doesn't have a copy
constructor. Any user defined type used with a standard container must
have a public copy constructor if any other constructor is declared
because the object is copied into the container.
--
Ian Collins.
thanks Ian Collins for replying.but when i used the standard string
class without any copy constructor for the student class it didn't give
me errors why is it giving error now.
thanks and regards
Ashwin

Jul 28 '06 #8
Ashwin wrote:
Ian Collins wrote:
>>Ashwin wrote:
>>>/usr/include/c++/3.2.2/bits/stl_construct.h :78: no matching function
for call
to `student::stude nt(const student&)'
assign2.cpp: 80: candidates are: student::studen t()
assign2.cpp: 8: student::studen t(student&)

The compiler is telling you that student doesn't have a copy
constructor . Any user defined type used with a standard container must
have a public copy constructor if any other constructor is declared
because the object is copied into the container.
--
Ian Collins.
It's standard practice on Usenet not to quote signatures (anything after
--).
>
thanks Ian Collins for replying.but when i used the standard string
class without any copy constructor for the student class it didn't give
me errors why is it giving error now.
Please try and fix your capitalisation, it will make your posts easier
to read.

I'm not sure what you mean, did you use std::string in place of you
student class? If so, std::string has a copy constructor. If not,
please post some code.

--
Ian Collins.
Jul 28 '06 #9

Ian Collins wrote:
Ashwin wrote:
Ian Collins wrote:
>Ashwin wrote:
/usr/include/c++/3.2.2/bits/stl_construct.h :78: no matching function
for call
to `student::stude nt(const student&)'
assign2.cpp:8 0: candidates are: student::studen t()
assign2.cpp:8 : student::studen t(student&)
The compiler is telling you that student doesn't have a copy
constructor. Any user defined type used with a standard container must
have a public copy constructor if any other constructor is declared
because the object is copied into the container.
--
Ian Collins.
It's standard practice on Usenet not to quote signatures (anything after
--).

thanks Ian Collins for replying.but when i used the standard string
class without any copy constructor for the student class it didn't give
me errors why is it giving error now.

Please try and fix your capitalisation, it will make your posts easier
to read.

I'm not sure what you mean, did you use std::string in place of you
student class? If so, std::string has a copy constructor. If not,
please post some code.

--
Ian Collins.
Thanks Ian Collins. I had used std:: string . Now i know where the
problem is.When i use my string class the compiler doesn't know how to
copy it to the container as it is not of standard type.Now if i write
the copy constructor for student class the compiler will know how to
load into the container, correct me if i am wrong.I hope your are happy
with the capitalisation.

Thanks and Regards
Ashwin

Jul 28 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5691
by: Julian | last post by:
I would like to have output from my program to be written to cout as well as a file. (actually, i want several other output options but this should explain my problem in the simplest way). I have seen commercial programs print output to the screen as well as to a log file. depending on the user and other situations, i might want to turn off one of the outputs or maybe even both outputs. so, i want a single line with operator << function...
10
1673
by: pmatos | last post by:
Hi all, I have the following code: class test { public: test(const std::string *n) : name(n) {} virtual ~test() {} const std::string * getName() { return name; }
3
1331
by: pmatos | last post by:
Hi all, I'm having a problem and for illustration purposes I developed code that shows what the problem is about. However, any comment on the code which is not directly about this issue is surely welcome. I have 3 classes, A is abstract, B and C inherit from A and then I create a vector with B's and C's and print them. Of course, I overload << for each one of them. So it is as follows:
2
1317
by: zeroYouMustNotSpamtype | last post by:
Hi, Describing this problem will be a bit long winded, but please bear with me: I've got three files in my project: permuts.h, permuts.cpp, and braids.cpp (some content from wich will eventually be moved into braids.h). Both cpp files include the h file. Permuts.h contains the Permutation class, for which I needed to overload <<. Doing this in that file caused an error(Duplicate definition I think - it occurs to
8
8386
by: jois.de.vivre | last post by:
Hi, I'm having some trouble overloading the << operator. I have the following, very simple code: #include <iostream> using namespace std; class test { private: int val;
3
1664
by: Suresh Tri | last post by:
Hi all, I was trying to overload '<' operator for (varchar,varchar). But in the function which handles the comparision I want to use the previous '<' operator.. but it is going into a recursion. My simplified code looks like : create or replace function orastringcmp (varchar, varchar) returns boolean as 'declare
1
2139
by: atomik.fungus | last post by:
Hi, as many others im making my own matrix class, but the compiler is giving me a lot of errors related to the friend functions which overload >> and <<.I've looked around and no one seems to get the same error. Here is the code of the class template< class T > class Matrix { friend ostream &operator << <>( ostream &, const Matrix< T > & ); friend istream &operator >> <>( istream &, Matrix< T > & ); public: ...
0
268
by: Ashwin | last post by:
FamilyNet International Newsgate From: "Ashwin" <ashwin.bevinje@gmail.com> Kai-Uwe Bux wrote: Thanks Kai-Uwe Bux. As you said the copy constructor is strudent(
8
1710
by: Goran | last post by:
Hi all, I have a question regarding operator <<. A lib of mine contains a class with an overloaded operator << as NON- class member. This would look like: #include <iostream> #include <libsomelib.h>
0
9922
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10702
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10801
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10391
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9536
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7935
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5963
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4584
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4177
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.