Oct 9, 2013 at 9:29am UTC
Solved.
Last edited on Oct 12, 2013 at 5:36am UTC
Oct 9, 2013 at 9:42am UTC
You did it again. Please look at code you had earlier and i provided link to and make operator<< exactly like that. In two places.
lines 47-49 will not be executed because return before them will stop function execution.
Oct 9, 2013 at 9:54am UTC
Solved.
Last edited on Oct 12, 2013 at 5:36am UTC
Oct 9, 2013 at 2:24pm UTC
@MiiNiPaa
Regarding
2) You cannot have friend definition inside class, only declaration.
Actually, this is perfectly valid in this case.
You are allowed to define a friend function within a class declaration (to quote the standard) "if and only if the class is a non-local class, the function name is unqualified, and the function has namespace scope."
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#include <iostream>
#include <string>
class Message {
private :
std::string m_text;
public :
Message() : m_text("Hello, world!" ) {
}
Message(const std::string text) : m_text(text) {
}
friend std::ostream& operator <<(std::ostream& os, const Message& msg) {
os << msg.m_text;
return os;
}
};
int main() {
Message greeting;
Message salutation("Bonjour le monde !" );
std::cout << greeting << std::endl;
std::cout << salutation << std::endl;
return 0;
}
Hello, world!
Bonjour le monde !
Andy
Last edited on Oct 9, 2013 at 2:25pm UTC
Oct 9, 2013 at 6:08pm UTC
Oh, so that's what you meant about the friend part of the code. Thanks a lot. I'm going to try to write the - (unary) part now.
Oct 9, 2013 at 7:53pm UTC
Solved.
Last edited on Oct 12, 2013 at 5:36am UTC
Oct 9, 2013 at 8:02pm UTC
Ok, problem with multiplication:
1 2 3 4 5 6
Complex& operator *=(const Complex& rhs)
{
this ->r = (this ->r * rhs.r) - (this ->i * rhs.i);
this ->i = (this ->r * rhs.i) + (this ->i * rhs.r);
return *this ; //↑ this.r is already changed on previous line
}
fix:
1 2 3 4 5 6 7
Complex& operator *=(const Complex& rhs)
{
int im = (this ->r * rhs.i) + (this ->i * rhs.r);
this ->r = (this ->r * rhs.r) - (this ->i * rhs.i);
this ->i = im;
return *this ;
}
Share your unary minus code.
Last edited on Oct 9, 2013 at 8:03pm UTC
Oct 9, 2013 at 8:06pm UTC
Solved.
Last edited on Oct 12, 2013 at 5:37am UTC
Oct 9, 2013 at 8:22pm UTC
Solved.
Last edited on Oct 12, 2013 at 5:37am UTC
Oct 9, 2013 at 8:22pm UTC
Line 140:
cout << "-" << ninth + tenth << " = " << -(ninth + eighth ) << endl;
Oct 9, 2013 at 8:28pm UTC
Oh it should add the tenth
Last edited on Oct 9, 2013 at 8:32pm UTC
Oct 9, 2013 at 8:35pm UTC
Ok, the only problem now is part 2 of - (unary) outputs -(1 + -2i) = (-1 + 2i) when it should output -(3+4i) = -3-4i
Oct 9, 2013 at 8:43pm UTC
You are providing second variable to it which is equal 1-2i. Provide some other and everything would be fine
Oct 9, 2013 at 8:50pm UTC
Solved.
Last edited on Oct 12, 2013 at 5:37am UTC
Oct 10, 2013 at 2:27am UTC
Can anyone help please, thanks.
Oct 10, 2013 at 4:09am UTC
Line 90: Complex third(1,-2);
Line 122: cout << "-" << third << " = " << -third << endl;
output should be -1 + 2i here. Everything is correct
Oct 10, 2013 at 4:37am UTC
Solved.
Last edited on Oct 12, 2013 at 5:37am UTC