-
October 12th, 2014, 12:09 PM
#1
Problems with operator overloading
I would like my program to do this:
Code:
Complex A(1,2);
cout << A;
Message in Console should be:
Why is my code not able to compile?
Code:
#include <iostream>
#include <map>
#include "funkcije.h"
#include "complex.h"
using namespace std;
int main()
{
Complex comp1(1,1);
Complex comp2(6,7);
Complex comp3;
cout << comp1 << '\n';
return 0;
compex.h
Code:
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
using namespace std;
class Complex
{
int r;
int j;
public:
Complex();
Complex(int a, int b);
Complex operator+ (const Complex &b1);
Complex operator- (const Complex &b);
Complex operator* (const Complex &b);
ostream& operator<< (ostream &stream, const Complex &nr);
Complex operator>> ( string s1 );
void get_r_j();
};
#endif // COMPLEX_H
complex.cpp
Code:
#include "complex.h"
Complex::Complex(){
r = 0;
j = 0;
}
Complex::Complex(int a, int b)
{
r = a;
j = b;
}
ostream& Complex::operator<< (ostream &stream, const Complex &nr){
cout << nr.r << " + " << nr.j << "j" << '\n';
return stream;
}
}
Last edited by flex567; October 12th, 2014 at 12:16 PM.
-
October 12th, 2014, 12:31 PM
#2
Re: Problems with operator overloading
operator<< should be declared to be a non-member function with two parameters:
Code:
std::ostream& operator<< (std::ostream &stream, const Complex &nr);
Since it has access to the private member variables of Complex, it should be declared a friend function of Complex.
At the moment, you are trying to declare it as a member function instead.
By the way, do not place using directives like using namespace std at file scope in a header file. Rather, fully qualify the names. Also, you only need to #include <iosfwd> in the header; you can #include <ostream> and <istream> in the source file.
-
October 12th, 2014, 03:24 PM
#3
Re: Problems with operator overloading
I would now like to overload >> operator.
I would like my program to do the fallowing:
Code:
Complex ab;
3 >> ab; //ab.r and ab.j become 3
This is the code for the operation:
main:
Code:
#include <iostream>
#include <map>
#include "funkcije.h"
#include "complex.h"
using namespace std;
int main()
{
3 >> comp3;
return 0;
}
Complex.h
Code:
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
using namespace std;
class Complex
{
int r;
int j;
public:
Complex();
Complex(int a, int b);
Complex operator+ (const Complex &b1);
Complex operator- (const Complex &b);
Complex operator* (const Complex &b);
// Complex operator+= (const Complex &b, int nr);
void operator>> (const Complex &ob, int nr);
void get_r_j();
friend ostream& operator<< (ostream &stream, const Complex &nr);
// friend istream& operator>>(istream& in, string s1);
};
#endif // COMPLEX_H
Complex.cpp
Code:
#include "complex.h"
void Complex::operator>> (const Complex &ob, int nr){
ob.j = nr;
ob.r = nr;
}
but it won't compile?
-
October 12th, 2014, 04:25 PM
#4
Re: Problems with operator overloading
The comments that laserlight gave for operator << also are relevant for operator >>
In the class Complex declaration:
Code:
friend std::istream & operator >> (std::istream & in , Complex & ob);
The global function could look like this:
Code:
std::istream & operator >> (std::istream & in , Complex & ob)
{
in >> ob.r >> ob.j;
return in;
}
usage:
Code:
Complex c;
cin >> c;
-
October 12th, 2014, 05:23 PM
#5
Re: Problems with operator overloading
In general, I would code the member functions in this order, when appropriate:
1) Constructor(s)
2) Copy Constructor
3) Copy assignment operator
4) destructor (if needed)
5) operator << (to help verify the first 4 are correct).
(You don't actually need both of the constructors, but it is fine to do it that way)
In your case, the default copy constructor and copy assignment operator are OK and do not need to be implemented.
Last edited by Philip Nicoletti; October 13th, 2014 at 06:39 AM.
Reason: Fixed ambiguous statement.
-
October 13th, 2014, 02:52 AM
#6
Re: Problems with operator overloading
This won't compile
Code:
std::istream & operator >> (std::istream & in , Complex & ob)
{
in >> ob.r >> ob.j;
return in;
}
error: Ambiguous overload for operator >>
-
October 13th, 2014, 03:29 AM
#7
Re: Problems with operator overloading
So you should start by implementing the following:
I already implemented them it is just that i didn't put it on this forum. This programs code is bigger than displayed on this forum.
-
October 13th, 2014, 04:06 AM
#8
Re: Problems with operator overloading
Originally Posted by flex567
This won't compile
Code:
std::istream & operator >> (std::istream & in , Complex & ob)
{
in >> ob.r >> ob.j;
return in;
}
error: Ambiguous overload for operator >>
What functions have you declared/defined for the operator >> ?
Note that there is already a STL complex class called complex. See http://www.cplusplus.com/reference/complex/
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 13th, 2014, 05:28 AM
#9
Re: Problems with operator overloading
Note that there is already a STL complex class called complex
I have renamed my class Complex to Complex1
-
October 13th, 2014, 05:35 AM
#10
Re: Problems with operator overloading
What functions have you declared/defined for the operator >> ?
Code:
#ifndef Complex1_H_
#define Complex1_H_
#include <iostream>
using namespace std;
class Complex1
{
int r;
int j;
public:
Complex1();
Complex1(int a, int b);
Complex1 operator+ (const Complex1 &b1);
Complex1 operator- (const Complex1 &b);
Complex1 operator* (const Complex1 &b);
Complex1 operator+= (int nr);
void get_r_j();
friend ostream& operator<< (ostream &stream, const Complex1 &nr);
friend istream& operator>> (istream& in, Complex1 &ob);
};
#endif /* Complex1_H_ */
Code:
#include "Complex1.h"
Complex1::Complex1(){
r = 0;
j = 0;
}
Complex1::Complex1(int a, int b)
{
r = a;
j = b;
}
Complex1 Complex1::operator+ (const Complex1 &b){
Complex1 bbb;
bbb.r = this->r + b.r;
bbb.j = this->j + b.j;
return bbb;
}
Complex1 Complex1::operator- (const Complex1 &b){
Complex1 bbb;
bbb.r = this->r - b.r;
bbb.j = this->j - b.j;
return bbb;
}
Complex1 Complex1::operator* (const Complex1 &b){
//http://www.clarku.edu/~djoyce/Complex1/mult.html
Complex1 bbb;
bbb.r = (this->r * b.r) + (this->j * b.j);
bbb.j = (this->j * b.r) + (this->r * b.j);
return bbb;
}
void Complex1::get_r_j(){
cout << r << " + " << j << "j" << '\n';
}
Complex1 Complex1::operator+= (int nr){
Complex1 bbb;
bbb.r = bbb.r + nr;
bbb.j = bbb.j + nr;
return bbb;
}
Code:
#ifndef FUNKCIJE_H_
#define FUNKCIJE_H_
#include <iostream>
#include <string>
#include <map>
using namespace std;
map<int,int> complex_nr(int a,int b);
map<int,int> complex_sum(map<int,int> a1,map<int,int> b1 );
#endif /* FUNKCIJE_H_ */
Code:
#include "funkcije.h"
#include "Complex1.h"
map<int,int> Complex1_nr(int a,int b){
map<int, int> temp;
temp[a] = b;
return temp;
}
map<int,int> Complex1_sum(map<int,int> a1, map<int,int> b1 ){
int a = 0;
map<int,int> temp2;
map<int,int>::iterator it1 = a1.begin();
map<int,int>::iterator it2 = b1.begin();
a = (*it1).first + (*it2).second;
temp2[a] = (*it1).second + (*it2).second;
return temp2;
}
ostream& operator<< (ostream &stream, const Complex1 &nr){
cout << nr.r << " + " << nr.j << "j" << '\n';
return stream;
}
istream& operator>>(istream &in, const Complex1 &ob){
in >> ob.r >> ob.j;
return in;
}
Code:
#include <iostream>
#include <map>
#include "funkcije.h"
#include "Complex1.h"
using namespace std;
int main()
{
Complex1 comp1(1,1);
Complex1 comp2(6,7);
Complex1 comp3;
Complex1 c_nr1,c_nr2,c_nr3;
c_nr1 = comp1 + comp2; // test +
c_nr2 = comp1 * comp2; // test *
c_nr3 = comp1 - comp2; // test -
cout << c_nr1 << '\n';
cout << c_nr2 << '\n';
cout << c_nr3 << '\n';
cin >> comp3;
cout << (comp1 += 8); //??
cout << comp1; // ??
return 0;
}
-
October 13th, 2014, 06:37 AM
#11
Re: Problems with operator overloading
When you implement operator >> you have:
Code:
istream& operator>>(istream &in, const Complex1 &ob){
It should not be const (since you are changing the value)
Last edited by Philip Nicoletti; October 13th, 2014 at 06:56 AM.
Reason: fixed typo
-
October 13th, 2014, 07:04 AM
#12
Re: Problems with operator overloading
Also, your arithmetic operators could be set up differently.
Here is an example using + and +=
1) make += a member function (returning Complex & ... not a copy)
2) make + a non-member function (which internally uses +=)
Code:
class Complex1
{
// your other methods
Complex1 & operator += (const Complex1 & rhs);
};
Complex1 & Complex1::operator += (const Complex1 & rhs)
{
r += rhs.r;
j += rhs.j;
return *this;
}
Complex1 operator + (const Complex1 & lhs , const Complex1 & rhs)
{
Complex1 temp(lhs);
return temp += rhs;
}
There are other ways to do operator +
Last edited by Philip Nicoletti; October 13th, 2014 at 12:37 PM.
Reason: correctly error pointed out by 2kaud
-
October 13th, 2014, 12:07 PM
#13
Re: Problems with operator overloading
Code:
Complex1 operator + (const Complex1 & lhs , const Complex1 & rhs)
{
Complex1 temp(rhs);
return temp += rhs;
}
rhs + rhs ???
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 13th, 2014, 12:26 PM
#14
Re: Problems with operator overloading
Originally Posted by flex567
I have renamed my class Complex to Complex1
There is no need to do that. The class template from the standard library is named complex, not Complex, so there is no name collision to begin with. Even if there was a name collision, it would be in the std namespace. If you would be more careful with your using directives, perhaps also declaring your own class within your own namespace, then there would be no problem. I have already told you that you should not have using directives at file scope in header files.
Rather, keep in mind that you would normally use std::complex instead of defining your own class, and perhaps look at the interface provided by std::complex for inspiration on your own practice complex number class.
-
October 13th, 2014, 12:36 PM
#15
Re: Problems with operator overloading
Originally Posted by 2kaud
Code:
Complex1 operator + (const Complex1 & lhs , const Complex1 & rhs)
{
Complex1 temp(rhs);
return temp += rhs;
}
rhs + rhs ???
oops. I do better to type into an editor first, instead of typing code directly.
I have corrected the error in my post. Obviously it should be:
Code:
Complex1 temp(lhs);
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|