+5 votes

#ifndef COMPLEX_H

#define COMPLEX_H

#include <iostream>

using std::ostream;

struct complex_number{

complex_number(int n): n(n){}

complex_number operator+(const complex_number &)const;

complex_number operator-(const complex_number &)const;

complex_number operator*(const complex_number &)const;

complex_number operator/(const complex_number &)const;

complex_number operator+(int) const;

complex_number operator-(int) const;

complex_number operator*(int) const;

complex_number operator/(int) const;

complex_number operator+(double) const;

complex_number operator-(double) const;

complex_number operator*(double) const;

complex_number operator/(double) const;

friend complex_number operator+(int, const complex_number &);

friend complex_number operator-(int, const complex_number &);

friend complex_number operator*(int, const complex_number &);

friend complex_number operator/(int, const complex_number &);

friend complex_number operator+(double, const complex_number &);

friend complex_number operator-(double, const complex_number &);

friend complex_number operator*(double, const complex_number &);

friend complex_number operator/(double, const complex_number &);

friend ostream &operator<<(ostream &, const complex_number &);

private:

int n;

};

#endif /* COMPLEX_H */

/* complex.cc */

#include <iostream>

struct complex_number operator+(int n, complex_number &o) {

return complex_number(o + n);

}

complex_number operator-(int n, complex_number &o) {

return complex_number(n) - o;

}

complex_number operator*(int n, complex_number &o) {

return complex_number (o * n);

}

complex_number operator/(int n, complex_number &o) {

return complex_number(n) / o;

}

ostream &operator<<(ostream &out, complex_number &o) {

return out<<o.n;

}

/* main.cc */

#include <iostream>

using std::cout;

using std::endl;

int main(void)

{

int a = 4;

int b = 3;

int c = 2;

int d = 1;

double n = 5.0;

cout << a + b << " + " << c + d << " = " << complex_number << endl;

cout << a + b << " - " << c + d << " = " << complex_number << endl;

cout << a + b << " * " << c + d << " = " << complex_number << endl;

cout << a + b << " / " << c + d << " = " << complex_number << endl;

cout << a + b << " + " << 5.0 << " = " << complex_number << endl;

cout << a + b << " - " << 5.0 << " = " << complex_number << endl;

cout << a + b << " * " << 5.0 << " = " << complex_number << endl;

cout << a + b << " / " << 5.0 << " = " << complex_number << endl;

cout << 5.0 << " + " << a + b << " = " << complex_number << endl;

cout << 5.0 << " - " << a + b << " = " << complex_number << endl;

cout << 5.0 << " * " << a + b << " = " << complex_number << endl;

cout << 5.0 << " / " << a + b << " = " << complex_number << endl;

return 0;

}

#define COMPLEX_H

#include <iostream>

using std::ostream;

struct complex_number{

complex_number(int n): n(n){}

complex_number operator+(const complex_number &)const;

complex_number operator-(const complex_number &)const;

complex_number operator*(const complex_number &)const;

complex_number operator/(const complex_number &)const;

complex_number operator+(int) const;

complex_number operator-(int) const;

complex_number operator*(int) const;

complex_number operator/(int) const;

complex_number operator+(double) const;

complex_number operator-(double) const;

complex_number operator*(double) const;

complex_number operator/(double) const;

friend complex_number operator+(int, const complex_number &);

friend complex_number operator-(int, const complex_number &);

friend complex_number operator*(int, const complex_number &);

friend complex_number operator/(int, const complex_number &);

friend complex_number operator+(double, const complex_number &);

friend complex_number operator-(double, const complex_number &);

friend complex_number operator*(double, const complex_number &);

friend complex_number operator/(double, const complex_number &);

friend ostream &operator<<(ostream &, const complex_number &);

private:

int n;

};

#endif /* COMPLEX_H */

/* complex.cc */

#include <iostream>

struct complex_number operator+(int n, complex_number &o) {

return complex_number(o + n);

}

complex_number operator-(int n, complex_number &o) {

return complex_number(n) - o;

}

complex_number operator*(int n, complex_number &o) {

return complex_number (o * n);

}

complex_number operator/(int n, complex_number &o) {

return complex_number(n) / o;

}

ostream &operator<<(ostream &out, complex_number &o) {

return out<<o.n;

}

/* main.cc */

#include <iostream>

using std::cout;

using std::endl;

int main(void)

{

int a = 4;

int b = 3;

int c = 2;

int d = 1;

double n = 5.0;

cout << a + b << " + " << c + d << " = " << complex_number << endl;

cout << a + b << " - " << c + d << " = " << complex_number << endl;

cout << a + b << " * " << c + d << " = " << complex_number << endl;

cout << a + b << " / " << c + d << " = " << complex_number << endl;

cout << a + b << " + " << 5.0 << " = " << complex_number << endl;

cout << a + b << " - " << 5.0 << " = " << complex_number << endl;

cout << a + b << " * " << 5.0 << " = " << complex_number << endl;

cout << a + b << " / " << 5.0 << " = " << complex_number << endl;

cout << 5.0 << " + " << a + b << " = " << complex_number << endl;

cout << 5.0 << " - " << a + b << " = " << complex_number << endl;

cout << 5.0 << " * " << a + b << " = " << complex_number << endl;

cout << 5.0 << " / " << a + b << " = " << complex_number << endl;

return 0;

}

0 votes

Look at all the warnings. They show the line numbers.

For your specific question:

main.cpp:68:19: error: ‘int complex_number::n’ is private within this context return out<<o.n; ^ main.cpp:38:9: note: declared private here int n; ^

So you're trying to access the field n of structure o (on line 68) However, (on line 38) n is declared to be private. Hence you have the error: you're trying to access a field (variable of an object/struct) that you have no permission to.

You tried to crate a friend function declaration:

friend ostream &operator<<(ostream &, const complex_number &);

, however during the implementation you missed the const from the second parameter, and now the signature of the two functions do not match, and your function is not a friend of the complex_number structure.

To fix it, use the correct signature:

```
ostream &operator<<(ostream &out, const complex_number &o) {
return out<<o.n;
}
```

After this, your code compiles, but does not link as none of the complex_number operators have been defined (only the friend functions, but not the ones that are part of the structure).

Some extra notes:

- You should keep your files separated. Do not copy everything into one giant file. Use multiple header and source files as the code structure demands.
- A complex number has two component: a real part and an imaginary part. Your complex_number only has one of them (so it's not a complex number in mathematical sense). Reference: https://en.wikipedia.org/wiki/Complex_number

The complex number (4 + 4i) + (2 + 1i) should be 6 + 5i, and the result from my output shows 11 as the answer. Am I doing wrong implementation for complex numbers?

I don't see that line of code. However, I think your problem is that you think (4 + 4) means the complex number 4, 4j to the C/C++ compiler. It does not. It means the integral number 4 + 4 = 8.

This is how you use std::complex: https://en.cppreference.com/w/cpp/numeric/complex

This is how you use std::complex: https://en.cppreference.com/w/cpp/numeric/complex

...

https://onlinegdb.com/iXRjD1DZy