For whatever reason, I'm having issues overloading the << operator. Here's what I have - any help would be appreciated. -
//Set.h
-
class Set{
-
.
-
.
-
.
-
}
-
ostream operator<<(ostream& out, const Set& s);
-
-
//Set.cpp
-
#include "Set.h"
-
.
-
.
-
.
-
void Set::print(ostream &out){
-
//sends 'out' a nicely formatted version of the set
-
}
-
.
-
.
-
.
-
ostream operator<< (ostream& out, const Set& s){
-
s.print(out);
-
return out;
-
}
-
Whenever I try to compile this, I get an error saying
"error C2662: 'Set::print' : cannot convert 'this' pointer from 'const Set' to 'Set &'"
I'm unsure what to do about this - any advice?
9 2349
I cant' figure out how to delete this....
Where is your problem?
Kind regards,
Dmjpro.
Where is your problem?
Kind regards,
Dmjpro.
Oh - sorry, I've posted my problem now.
Any suggestions??
For whatever reason, I'm having issues overloading the << operator. Here's what I have - any help would be appreciated. -
//Set.h
-
class Set{
-
.
-
.
-
.
-
}
-
ostream operator<<(ostream& out, const Set& s);
-
-
//Set.cpp
-
#include "Set.h"
-
.
-
.
-
.
-
void Set::print(ostream &out){
-
//sends 'out' a nicely formatted version of the set
-
}
-
.
-
.
-
.
-
ostream operator<< (ostream& out, const Set& s){
-
s.print(out);
-
return out;
-
}
-
Whenever I try to compile this, I get an error saying
"error C2662: 'Set::print' : cannot convert 'this' pointer from 'const Set' to 'Set &'"
I'm unsure what to do about this - any advice?
Using code tags, post whole code.
Kind regards,
Dmjpro.
Using code tags, post whole code.
Kind regards,
Dmjpro.
Alright:
//SetGE.h -
#include<iostream>
-
using namespace std;
-
-
class SetGE {
-
public:
-
SetGE();
-
void add(int);
-
void add(int[], int);
-
void remove(int);
-
bool elementOf(int) const;
-
SetGE setUnion(const SetGE&) const;
-
SetGE intersection(const SetGE&) const;
-
SetGE difference(const SetGE&) const;
-
void makeEmpty();
-
bool isEmpty();
-
bool subset(const SetGE&) const;
-
bool equals(const SetGE&) const;
-
void print(ostream&) const;
-
-
private:
-
int set[100];
-
int size;
-
};
-
-
SetGE operator+(const SetGE& lhs, const SetGE& rhs);
-
SetGE operator-(const SetGE& lhs, const SetGE& rhs);
-
bool operator<=(const SetGE& lhs, const SetGE& rhs);
-
bool operator==(const SetGE& lhs, const SetGE& rhs);
-
ostream operator<<(ostream& out, const SetGE& s);
-
//SetGE.cpp
Use Code Tags...It will be code=cpp -
ostream operator<< (ostream &out, const SetGE& s){
-
//make it...ostream operator<< (ostream &out, SetGE& s)
-
s.print(out);
-
return out;
-
}
-
I think it will work.
Kind regards,
Dmjpro.
Use Code Tags...It will be code=cpp -
ostream operator<< (ostream &out, const SetGE& s){
-
//make it...ostream operator<< (ostream &out, SetGE& s)
-
s.print(out);
-
return out;
-
}
-
I think it will work.
Kind regards,
Dmjpro.
removing the const (as well as making print const) gives me the following crazy error:
1>c:\program files\microsoft visual studio 8\vc\include\ostream(587) : error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\ios(151) : see declaration of 'std::basic_ios<_Elem,_Traits>::basic_ios'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> This diagnostic occurred in the compiler generated function 'std::basic_ostream<_Elem,_Traits>::basic_ostream( const std::basic_ostream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
And I have no idea what to do with that.
You have a couple of issues with this code. Let's first take a look at the object and then we'll take a look at overloading the operator<<.
It looks like you're dealing with 2 objects: Set and SetGE. Is SetGE supposed to be derived from Set or is it a stand alone object? I'm going to assume SetGE is standalone.
The second problem you have is that you don't declare operator<< as a friend and return by value instead of reference. This might be the reason for the private access error. You're code should look like: - friend ostream & operator<<( ostream &, const SetGE &);
Finally, if you pass a const SetGE, then you have to make sure all the object methods called are also const.
You have a couple of issues with this code. Let's first take a look at the object and then we'll take a look at overloading the operator<<.
It looks like you're dealing with 2 objects: Set and SetGE. Is SetGE supposed to be derived from Set or is it a stand alone object? I'm going to assume SetGE is standalone.
The second problem you have is that you don't declare operator<< as a friend and return by value instead of reference. This might be the reason for the private access error. You're code should look like: - friend ostream & operator<<( ostream &, const SetGE &);
Finally, if you pass a const SetGE, then you have to make sure all the object methods called are also const.
Ah, that does seem to fix the problem - thanks! Now, though, I'd like to know a bit more 'bout why.
To answer your first question 'Set' on its own doesn't actually exist. SetGE is the name of the class - I just though 'Set' made more sense when I wasn't pasting the full code. Sorry 'bout that.
It actually seems to work even without declaring it as a friend. To get the code to work, just returning the ostream my reference did the trick. However, while I understand the importance of passing the ostream by reference, why do I need to return it by reference as well?
However, while I understand the importance of passing the ostream by reference, why do I need to return it by reference as well?
It is not required to return the ostream by reference. However, unless you return the ostream&, then code like this won't work: -
SetGE obj;
-
cout << "The result is " << obj << endl;
-
As far as the friend goes, that is not required unless the function requires access to the private data variables.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: |
last post by:
I have not posted to comp.lang.c++ (or comp.lang.c++.moderated)
before. In general when I have a C++ question I look for answers in
"The C++ Programming Language, Third Edition" by Stroustrup....
|
by: victor75040 |
last post by:
Before you all start flaming me, I am not a student and this is not
for any homework. Just someone learing c++ on their own.
I am now up to the chapter in my book that describes operator...
|
by: Bo Sun |
last post by:
hi:
in the following code:
class plus{
int data_item;
public:
|
by: pmatos |
last post by:
Hi all,
I'm overloading operator<< for a lot of classes. The question is about
style. I define in each class header the prototype of the overloading
as a friend. Now, where should I define the...
|
by: Tony Johansson |
last post by:
Hello!
I have this wrapper class Integer below that I use when testing operator
overloading.
A book that I read say that the expression
Integer i;
i+5 is translated to operator+(i,5) using the...
|
by: Eckhard Lehmann |
last post by:
Hi,
I try to recall some C++ currently. Therefore I read the "Standard C++
Bible" by C. Walnum, A. Stevens and - of course there are chapters about
operator overloading.
Now I have a class...
|
by: jay |
last post by:
In the c++ primer ,i get a program.
A class's name is TT,and it define the operator overload!
TT first; //constructor
TT second(30);//constructor
TT thrid(40://constructor...
|
by: Jerry Fleming |
last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone
says that the four operators can only be overloaded with class member
functions instead of global (friend) functions: (), ,...
|
by: Rahul |
last post by:
Hi Everyone,
I was just overloading operator = for a class and i have a problem in
one case...
class A
{
A& operator=(const A& obj)
{
return *this;
|
by: Thomas Lenz |
last post by:
The code below should allow to use a comma instead of << with ostreams and
include a space between two operands when comma is used. e.g.
cout << "hello", "world", endl;
should print the line...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
| |