Solved.

Pages: 12
1
2
3
        this->r *= rhs.r;
        this->i *= rhs.r;
        return *this;
is not a rule for multiplication

Impossible. Are you sure that you did as in your previous code I even provided link to? Declaration in class, definition outside?

Look ↑
Solved.
Last edited on
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.
Solved.
Last edited on
Why did you change addition and deduction assigment operators? They were completely fine. And you still made the same mistake with friend function declaration.

Ok, I officially give up. Here is the fixed code:
http://pastebin.com/Ae9xDUFg
It only lacks unari minus function and code that uses it was commented so it will not prevent compiling.
It compiles and works. You can look how you should make friend functions.
@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
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.
Solved.
Last edited on
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
Solved.
Last edited on
Solved.
Last edited on
Line 140:
cout << "-" << ninth + tenth << " = " << -(ninth + eighth) << endl;
Oh it should add the tenth
Last edited on
Ok, the only problem now is part 2 of - (unary) outputs -(1 + -2i) = (-1 + 2i) when it should output -(3+4i) = -3-4i
You are providing second variable to it which is equal 1-2i. Provide some other and everything would be fine
Solved.
Last edited on
Can anyone help please, thanks.
Line 90: Complex third(1,-2);
Line 122: cout << "-" << third << " = " << -third << endl;
output should be -1 + 2i here. Everything is correct
Solved.
Last edited on
Topic archived. No new replies allowed.
Pages: 12