Не компилируется код класса, разделённого на файлы .cpp и .h | C++
Создал простой класс, представляющий комплексное число. Пытаюсь добавить перезагрузку для вывода, но выдаёт гору ошибок:
Complex.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);
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.
Ответы (1 шт):
Автор решения: Андрій Матяшов
→ Ссылка
#include iostream из .cpp перекинь в .h, ибо оно не определяет ostream в .h файле.
