problem regarding overloading of operator <<.

A

Ashwin

hi guys,

i have overloaded the << operator.as shown below.
ostream& operator<<(ostream &out, const student &a)
{
out<<a.idno;
out<< " " ;
// out<< a.name;
out<< " " ;
// out<< a.marks << endl;
return out;
}
and
ostream& operator<<(ostream &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_ostream said:
>& << 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.
 
A

amirkam1

Hi,
ostream& operator<<(ostream &out, const student &a)
You are accepting the reference as const .
ostream& operator<<(ostream &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<<(ostream &out,const string1 &s1)
It should work.

Regards,
Amir
 
K

Kai-Uwe Bux

Ashwin said:
hi guys,

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

This should read:

stream& operator<<(ostream &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

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
 
A

Ashwin

Hi,

You are accepting the reference as const .

this is just reference.

As "a" is const, this call is not working with overloaded operator
accepting non-const reference to string1.

Use this => ostream& operator<<(ostream &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
 
A

Ashwin

Kai-Uwe Bux said:
This should read:

stream& operator<<(ostream &out,string1 const &s1)


Is it intentional that your string class does not allow for embedded 0
characters within strings?


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.


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
 
A

Ashwin

Ashwin said:
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_node<_Tp>* std::list<_Tp, _Alloc>::_M_create_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_iterator<_Tp, _Tp&, _Tp*> std::list<_Tp,
_Alloc>::insert(std::_List_iterator<_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_back(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::student(const student&)'
assign2.cpp:80: candidates are: student::student()
assign2.cpp:8: student::student(student&)

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

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

kindly explain me this error.
 
I

Ian Collins

Ashwin said:
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_node<_Tp>* std::list<_Tp, _Alloc>::_M_create_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_iterator<_Tp, _Tp&, _Tp*> std::list<_Tp,
_Alloc>::insert(std::_List_iterator<_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_back(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::student(const student&)'
assign2.cpp:80: candidates are: student::student()
assign2.cpp:8: student::student(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.
 
A

Ashwin

Ian said:
Ashwin said:
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_node<_Tp>* std::list<_Tp, _Alloc>::_M_create_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_iterator<_Tp, _Tp&, _Tp*> std::list<_Tp,
_Alloc>::insert(std::_List_iterator<_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_back(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::student(const student&)'
assign2.cpp:80: candidates are: student::student()
assign2.cpp:8: student::student(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.

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
 
I

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.
 
A

Ashwin

Ian said:
It's standard practice on Usenet not to quote signatures (anything after
--).


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.

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
 
K

Kai-Uwe Bux

Ashwin said:
Ian said:
Ashwin said:
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_node<_Tp>* std::list<_Tp, _Alloc>::_M_create_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_iterator<_Tp, _Tp&, _Tp*> std::list<_Tp,
_Alloc>::insert(std::_List_iterator<_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_back(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::student(const student&)'
assign2.cpp:80: candidates are: student::student()
assign2.cpp:8: student::student(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.

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.

The error is somewhere in the code you did not post. Read the FAQ about how
to post. It will tell you to provide enough code for us to reproduce the
problem.

If it works with std::string instead of string1, then chances are, your
string1 class is broken and we might be seeing errors that manifest
themselves elsewhere.

Also, I doubt that you did not declare a copy constructor for student. The
compiler tells us that there is a constructor:

student::student(student&);

which qualifies as a copy constructor. However, it is not const-correct. Try

strudent( student const & );

instead.


Also, does your string1 class have a copy constructor? Is it written
const-correctly?


Best

Kai-Uwe Bux
 
I

Ian Collins

Ashwin said:
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.
Yes, or more precisely, know how to copy your class.

Well done on the capitalisation, one more small detail: 'I' is always
capitalised and you should add a space after each full stop (.).

Good luck,
 
A

Ashwin

Kai-Uwe Bux said:
Ashwin said:
Ian said:
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_node<_Tp>* std::list<_Tp, _Alloc>::_M_create_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_iterator<_Tp, _Tp&, _Tp*> std::list<_Tp,
_Alloc>::insert(std::_List_iterator<_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_back(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::student(const student&)'
assign2.cpp:80: candidates are: student::student()
assign2.cpp:8: student::student(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.

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.

The error is somewhere in the code you did not post. Read the FAQ about how
to post. It will tell you to provide enough code for us to reproduce the
problem.

If it works with std::string instead of string1, then chances are, your
string1 class is broken and we might be seeing errors that manifest
themselves elsewhere.

Also, I doubt that you did not declare a copy constructor for student. The
compiler tells us that there is a constructor:

student::student(student&);

which qualifies as a copy constructor. However, it is not const-correct. Try

strudent( student const & );

instead.


Also, does your string1 class have a copy constructor? Is it written
const-correctly?


Best

Kai-Uwe Bux

Thanks Kai-Uwe Bux. As you said the copy constructor is strudent(
student & ) if i use const qualifier there will be errors as i
haven't used const in the copy constuctor of string1 .so i have to
change many things in my string and linked list class.I have not used
this const qualifier regulerly so these many problems. Thanks for
replying .

Thanks and Regards
Ashwin
 
I

Ian Collins

Whoops, I didn't spot that, not used to gcc error messages :)
Thanks Kai-Uwe Bux. As you said the copy constructor is strudent(
student & ) if i use const qualifier there will be errors as i
haven't used const in the copy constuctor of string1 .so i have to
change many things in my string and linked list class.I have not used
this const qualifier regulerly so these many problems. Thanks for
replying .
Copy constructors should always take a const reference as their
parameter, otherwise you will run into the exact problem you have here.
If you are not going to modify the passed value, this should be const.
 
A

Ashwin

Ian said:
Whoops, I didn't spot that, not used to gcc error messages :)

Copy constructors should always take a const reference as their
parameter, otherwise you will run into the exact problem you have here.
If you are not going to modify the passed value, this should be const.

Hi Ian Collins.As you and Kai-Uwe Bux said I have used the const
qualifier now . I have not modified it's valu but if I can another
function of the same class it is giving me errors please see the code
below.
string1::string1(const string1 &s)
{
str=new linkedlist<char>;
int i;
for(i=0;i<s.len();i++)
{
str->push_back(s);
}
length=i;
}
i have added the const qualifier.

char string1::eek:perator[](unsigned int i)
{
char ch='\0';
if(empty())
{
cout<<"no data available"<<endl;
return ch;
}
else
{
linkedlist<char>::iterator m;
str->begin(m);
for(int j=0;j!=i;j++)
{
++m;
}
return *m;
}
}
i haven't changed the value of s stored as a liked list but just
passing through the linked list using iterator and returning the
character
now this is the error

string1.h: In copy constructor `string1::string1(const string1&)':
string1.h:53: passing `const string1' as `this' argument of `int
string1::len()
' discards qualifiers
string1.h:55: passing `const string1' as `this' argument of `char
string1::eek:perator[](unsigned int)' discards qualifiers
string1.h: In member function `int string1::eek:perator=(const string1&)':
string1.h:77: passing `const string1' as `this' argument of `int
string1::len()
' discards qualifiers
string1.h:79: passing `const string1' as `this' argument of `char
string1::eek:perator[](unsigned int)' discards qualifiers
assign2.cpp: In function `int main()':
assign2.cpp:87: no matching function for call to `student::student()'
assign2.cpp:16: candidates are: student::student(const student&)

so what should I do now. I was previously working on turbo c++ (not
standardised ) in that I didnot have these kind of problems.Please
suggest me some good books to get accustomed with this standardized
format of c++.

Thanks and regards
Ashwin
 
I

Ian Collins

Ashwin said:
Hi Ian Collins.As you and Kai-Uwe Bux said I have used the const
qualifier now . I have not modified it's valu but if I can another
function of the same class it is giving me errors please see the code
below.
string1::string1(const string1 &s)
{
str=new linkedlist<char>;
int i;
for(i=0;i<s.len();i++)
{
str->push_back(s);
}
length=i;
}
i have added the const qualifier.

char string1::eek:perator[](unsigned int i)
{
char ch='\0';
if(empty())
{
cout<<"no data available"<<endl;
return ch;
}
else
{
linkedlist<char>::iterator m;
str->begin(m);
for(int j=0;j!=i;j++)
{
++m;
}
return *m;
}
}
i haven't changed the value of s stored as a liked list but just
passing through the linked list using iterator and returning the
character
now this is the error

string1.h: In copy constructor `string1::string1(const string1&)':
string1.h:53: passing `const string1' as `this' argument of `int
string1::len()


string1::len() should be declared const, it doesn't change the object.
' discards qualifiers
string1.h:55: passing `const string1' as `this' argument of `char
string1::eek:perator[](unsigned int)' discards qualifiers
string1.h: In member function `int string1::eek:perator=(const string1&)':
string1.h:77: passing `const string1' as `this' argument of `int
string1::len()
' discards qualifiers

string1::eek:perator[](unsigned int) should be declared const, it doesn't
change the object.
string1.h:79: passing `const string1' as `this' argument of `char
string1::eek:perator[](unsigned int)' discards qualifiers
assign2.cpp: In function `int main()':
assign2.cpp:87: no matching function for call to `student::student()'
assign2.cpp:16: candidates are: student::student(const student&)
student doesn't have default constructor.
so what should I do now. I was previously working on turbo c++ (not
standardised ) in that I didnot have these kind of problems.Please
suggest me some good books to get accustomed with this standardized
format of c++.
What better than the one and only "The C++ Programming Language", 3rd
edition?
 
A

Ashwin

Ian said:
Ashwin said:
Hi Ian Collins.As you and Kai-Uwe Bux said I have used the const
qualifier now . I have not modified it's valu but if I can another
function of the same class it is giving me errors please see the code
below.
string1::string1(const string1 &s)
{
str=new linkedlist<char>;
int i;
for(i=0;i<s.len();i++)
{
str->push_back(s);
}
length=i;
}
i have added the const qualifier.

char string1::eek:perator[](unsigned int i)
{
char ch='\0';
if(empty())
{
cout<<"no data available"<<endl;
return ch;
}
else
{
linkedlist<char>::iterator m;
str->begin(m);
for(int j=0;j!=i;j++)
{
++m;
}
return *m;
}
}
i haven't changed the value of s stored as a liked list but just
passing through the linked list using iterator and returning the
character
now this is the error

string1.h: In copy constructor `string1::string1(const string1&)':
string1.h:53: passing `const string1' as `this' argument of `int
string1::len()


string1::len() should be declared const, it doesn't change the object.
' discards qualifiers
string1.h:55: passing `const string1' as `this' argument of `char
string1::eek:perator[](unsigned int)' discards qualifiers
string1.h: In member function `int string1::eek:perator=(const string1&)':
string1.h:77: passing `const string1' as `this' argument of `int
string1::len()
' discards qualifiers

string1::eek:perator[](unsigned int) should be declared const, it doesn't
change the object.
string1.h:79: passing `const string1' as `this' argument of `char
string1::eek:perator[](unsigned int)' discards qualifiers
assign2.cpp: In function `int main()':
assign2.cpp:87: no matching function for call to `student::student()'
assign2.cpp:16: candidates are: student::student(const student&)
student doesn't have default constructor.
so what should I do now. I was previously working on turbo c++ (not
standardised ) in that I didnot have these kind of problems.Please
suggest me some good books to get accustomed with this standardized
format of c++.
What better than the one and only "The C++ Programming Language", 3rd
edition?

Thanks Collins . I have mada all the changes and the code is working
now . Thanks for the help
you all provided.
Thanks and Regards
Ashwin
 
I

Ian Collins

Ashwin said:
Thanks Collins . I have mada all the changes and the code is working
now . Thanks for the help
you all provided.

Good. Now all you have to do is practice trimming your responses!
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top