I'm completely stumped as to why I can't get the ostream& to run correctly.. I think it may be the simple math I did in the main function but I'm fairly certain it should be okay because of all the operator functions correct? Also for the first return where I have the (-1) before z.getImag, I'm not sure if I can do that or not... Any help would be greatly appreciated!
ostream& operator<<(ostream& out, Complex z) { // Write z to the output stream out
if (z.getImag < 0) {
return out << z.getReal << " - " << (-1)z.getImag << "i "if (z.getImag > 0) {
return out << z.getReal << " - " << z.getImag << "i " << cout << endl;
}
};
A few issues:
(-1)z.getImag
1. Your suspicion is warranted, this just simply isn't valid C++ syntax.
2. What happens when z.getImag == 0?
3. << cout <<endl; }
What is this guy doing here?
4. getImage is a function. It needs to be called with parentheses.
5. Don't put a endl or any newlines in there, that's normally up to the caller.
Just like how calling
1 2
int i;
cout << i;
doesn't automatically insert a newline.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
ostream& operator<<(ostream& out, Complex z) { // Write z to the output stream out
if (z.getImag() < 0.0)
{
return out << z.getReal() << " - " << -1.0 * z.getImag() << "i";
}
elseif (z.getImag() > 0.0)
{
return out << z.getReal() << " + " << z.getImag() << "i";
}
else
{
return out << z.getReal(); // up to you if you want to explicitly say 0i
}
}