Ошибка при компиляции
main.cpp
#include <iostream>
#include "complex.h"
using namespace std;
int main()
{
Complex x;
Complex y(4.3, 8.2);
Complex z(3.3, 1.1);
cout << x << y << z;
x = y * z;
cout << "Result: " << x << endl;
return 0;
}
complex.h
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
using namespace std;
class Complex
{
friend ostream &operator << (ostream &, const Complex &);
friend istream &operator >> (istream &, Complex &);
public:
Complex(double = 0.0, double= 0.0);
Complex operator+(const Complex &) const;
Complex operator-(const Complex &) const;
const Complex operator*(const Complex &) const;
const Complex &operator=(const Complex &);
private:
double real;
double imaginary;
};
#endif // COMPLEX_H
complex.cpp
#include <iostream>
#include "complex.h"
Complex::Complex(double realPart, double imaginaryPart)
:real(realPart), imaginary(imaginaryPart)
{
}
Complex Complex::operator + (const Complex &operand2) const
{
return Complex(real + operand2.real, imaginary + operand2.imaginary);
}
Complex Complex::operator - (const Complex &operand2) const
{
return Complex(real - operand2.real, imaginary - operand2.imaginary);
}
const Complex Complex::operator*(const Complex &number) const
{
return Complex(real * number.real, imaginary * number.imaginary);
}
const Complex &Complex::operator=(const Complex &number)
{
real = number.real;
imaginary = number.imaginary;
return *this;
}
ostream &operator << (ostream &output, const Complex &number)
{
output << "The real part is: " << number.real << "\nThe imaginary part is: " << number.imaginary << "\n" << endl;
}
istream &operator >> (istream &input, Complex &number)
{
cout << "Please enter real part of your number: ";
input >> number.real;
cout << "Please enter imaginary part of your number: ";
input >> number.imaginary;
}
ошибка:
R:\Store\ToDo\examples\main.cpp:13: error: undefined reference to `Complex::operator*(Complex) const'
если передавать параметр в перегруженную операцию умножения не по ссылке, то программа работает. Почему нельзя передать объект по ссылке?