473,385 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Operator Overloading question

For whatever reason, I'm having issues overloading the << operator. Here's what I have - any help would be appreciated.
Expand|Select|Wrap|Line Numbers
  1. //Set.h
  2. class Set{
  3. .
  4. .
  5. .
  6. }
  7. ostream operator<<(ostream& out, const Set& s);
  8.  
  9. //Set.cpp
  10. #include "Set.h"
  11. .
  12. .
  13. .
  14. void Set::print(ostream &out){
  15. //sends 'out' a nicely formatted version of the set
  16. }
  17. .
  18. .
  19. .
  20. ostream operator<< (ostream& out, const Set& s){
  21.     s.print(out);
  22.     return out;
  23. }
  24.  
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?
Sep 11 '07 #1
9 2349
dmjpro
2,476 2GB
I cant' figure out how to delete this....
Where is your problem?

Kind regards,
Dmjpro.
Sep 11 '07 #2
Where is your problem?

Kind regards,
Dmjpro.
Oh - sorry, I've posted my problem now.

Any suggestions??
Sep 11 '07 #3
dmjpro
2,476 2GB
For whatever reason, I'm having issues overloading the << operator. Here's what I have - any help would be appreciated.

Expand|Select|Wrap|Line Numbers
  1. //Set.h
  2. class Set{
  3. .
  4. .
  5. .
  6. }
  7. ostream operator<<(ostream& out, const Set& s);
  8.  
  9. //Set.cpp
  10. #include "Set.h"
  11. .
  12. .
  13. .
  14. void Set::print(ostream &out){
  15. //sends 'out' a nicely formatted version of the set
  16. }
  17. .
  18. .
  19. .
  20. ostream operator<< (ostream& out, const Set& s){
  21.     s.print(out);
  22.     return out;
  23. }
  24.  
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.
Sep 11 '07 #4
Using code tags, post whole code.

Kind regards,
Dmjpro.
Alright:

//SetGE.h
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class SetGE {
  5. public:
  6.     SetGE();
  7.     void add(int);
  8.     void add(int[], int);
  9.     void remove(int);
  10.     bool elementOf(int) const;
  11.     SetGE setUnion(const SetGE&) const;
  12.     SetGE intersection(const SetGE&) const;
  13.     SetGE difference(const SetGE&) const;
  14.     void makeEmpty();
  15.     bool isEmpty();
  16.     bool subset(const SetGE&) const;
  17.     bool equals(const SetGE&) const;
  18.     void print(ostream&) const;
  19.  
  20. private:
  21.     int set[100];
  22.     int size;
  23. };
  24.  
  25. SetGE operator+(const SetGE& lhs, const SetGE& rhs);
  26. SetGE operator-(const SetGE& lhs, const SetGE& rhs);
  27. bool operator<=(const SetGE& lhs, const SetGE& rhs);
  28. bool operator==(const SetGE& lhs, const SetGE& rhs);
  29. ostream operator<<(ostream& out, const SetGE& s);
  30.  
//SetGE.cpp
Expand|Select|Wrap|Line Numbers
  1. #include "SetGE.h"
  2. #include <ostream>
  3.  
  4. SetGE::SetGE(){
  5.     size = 0;
  6. }
  7.  
  8. void SetGE::add(int i) {
  9.     if (!elementOf(i)){//only adds if that value isn't already in the set
  10.         set[size] = i;
  11.         size++;
  12.     }
  13. }
  14.  
  15. void SetGE::add(int a[], int n){
  16.     for (int i=0; i<n; i++){
  17.         add(a[i]);
  18.     }
  19. }
  20.  
  21. void SetGE::remove(int i){
  22.     int index = -1;
  23.     for (int j=0; j<size; j++){
  24.         if (set[j] == i){
  25.             index = j;
  26.             break;
  27.         }
  28.     }
  29.     if (index != -1){ //if i was found in the set
  30.         for (int j=index+1; j<size; j++)
  31.             set[j-1] = set[j];
  32.         size--;
  33.     }
  34. }
  35.  
  36. bool SetGE::elementOf(int i) const{
  37.     for (int j=0; j<size; j++)
  38.         if (set[j] == i)
  39.             return true;
  40.     return false;
  41. }
  42.  
  43. SetGE SetGE::setUnion(const SetGE& s2) const{
  44.     SetGE result;
  45.     for (int i=0; i<size; i++)
  46.         result.add(set[i]);
  47.     for (int i=0; i<s2.size; i++)
  48.         result.add(s2.set[i]);
  49.     return result;
  50. }
  51.  
  52. SetGE SetGE::intersection(const SetGE& s2) const{
  53.     SetGE result;
  54.     for (int i=0; i<size; i++){
  55.         for (int j=0; j<s2.size; j++){
  56.             if (set[i] == s2.set[j])
  57.                 result.add(set[i]);
  58.         }
  59.     }
  60.     return result;
  61. }
  62.  
  63. SetGE SetGE::difference(const SetGE& s2) const{
  64.     SetGE result;
  65.     for (int i=0; i<size; i++)
  66.         result.add(set[i]);
  67.     for (int i=0; i<s2.size; i++)
  68.         result.remove(s2.set[i]);
  69.     return result;
  70. }
  71.  
  72. void SetGE::makeEmpty(){
  73.     size = 0;
  74. }
  75.  
  76. bool SetGE::isEmpty(){
  77.     return size==0;
  78. }
  79.  
  80. bool SetGE::subset(const SetGE& s2) const{
  81.     for (int i=0; i<s2.size; i++)
  82.         if (!elementOf(s2.set[i]))
  83.             return false;
  84.     return true;
  85. }
  86.  
  87. bool SetGE::equals(const SetGE& s2) const{
  88.     if (subset(s2) && s2.size == size)//couldn't figure out how to get s2.subset(this) to work
  89.         return true;
  90.     return false;
  91. }
  92.  
  93. void SetGE::print(ostream &out) const{
  94.     out<<"{";
  95.     for (int i=0; i<size-1; i++) //prints # followed by comma
  96.         out<<set[i]<<",";
  97.  
  98.     if (size != 0)    //final # isn't followed by a comma
  99.         out<<set[size-1]<<"}";
  100.     else
  101.         out<<"}";
  102. }
  103.  
  104. SetGE operator+ (const SetGE& lhs, const SetGE& rhs){
  105.     return lhs.setUnion(rhs);
  106. }
  107.  
  108. SetGE operator- (const SetGE& lhs, const SetGE& rhs){
  109.     return lhs.difference(rhs);
  110. }
  111.  
  112. bool operator<= (const SetGE& lhs, const SetGE& rhs){
  113.     return rhs.subset(lhs);
  114. }
  115.  
  116. bool operator== (const SetGE& lhs, const SetGE& rhs){
  117.     return lhs.equals(rhs);
  118. }
  119.  
  120. ostream operator<< (ostream &out, const SetGE& s){
  121.     s.print(out);
  122.     return out;
  123. }
  124.  
Sep 11 '07 #5
dmjpro
2,476 2GB
Use Code Tags...It will be code=cpp
Expand|Select|Wrap|Line Numbers
  1. ostream operator<< (ostream &out, const SetGE& s){
  2. //make it...ostream operator<< (ostream &out, SetGE& s)
  3.     s.print(out);
  4.     return out;
  5. }
  6.  
I think it will work.

Kind regards,
Dmjpro.
Sep 11 '07 #6
Use Code Tags...It will be code=cpp
Expand|Select|Wrap|Line Numbers
  1. ostream operator<< (ostream &out, const SetGE& s){
  2. //make it...ostream operator<< (ostream &out, SetGE& s)
  3.     s.print(out);
  4.     return out;
  5. }
  6.  
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.
Sep 11 '07 #7
RRick
463 Expert 256MB
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:
Expand|Select|Wrap|Line Numbers
  1. 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.
Sep 11 '07 #8
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:
Expand|Select|Wrap|Line Numbers
  1. 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?
Sep 11 '07 #9
weaknessforcats
9,208 Expert Mod 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. SetGE obj;
  2. cout << "The result is " << obj << endl;
  3.  
As far as the friend goes, that is not required unless the function requires access to the private data variables.
Sep 11 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

30
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....
2
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...
2
by: Bo Sun | last post by:
hi: in the following code: class plus{ int data_item; public:
2
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...
1
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...
7
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...
6
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...
5
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: (), ,...
24
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;
3
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...
0
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...
0
isladogs
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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 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.