创建了一个表示复数的简单类。我正在尝试为输出添加重新加载,但它会产生大量错误:
复杂的.h:
#pragma once
class Complex
{
public:
Complex(float real, float imagine);
float get_real() const;
float get_imagine() const;
Complex operator+(const Complex& other) const;
Complex operator-(const Complex& other) const;
Complex operator*(const Complex& other) const;
private:
float real, imagine;
};
std::ostream& operator<<(std::ostream& out, const Complex& complex);
复杂的.cpp
#include "Complex.h"
#include <iostream>
Complex::Complex(float real = 0, float imagine = 0)
{
this->real = real;
this->imagine = imagine;
}
float Complex::get_real() const
{
return real;
}
float Complex::get_imagine() const
{
return imagine;
}
Complex Complex::operator+(const Complex& other) const
{
return Complex(real + other.real, imagine + other.imagine);
}
Complex Complex::operator-(const Complex& other) const
{
return Complex(real - other.real, imagine - other.imagine);
}
Complex Complex::operator*(const Complex& other) const
{
return Complex(real * other.real - imagine * other.imagine,
real * other.imagine + imagine * other.real);
}
std::ostream& operator<<(std::ostream& out, const Complex& complex)
{
return out << '(' << complex.get_real() << ", " << complex.get_imagine() << "i)";
}
main.cpp:标准与hello world
.