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