0

Пишу реализацию класса комплексных чисел на c++. Все работало с типом float, попытался добавить шаблоны и компилироваться перестало. Помогите(

//complex_class.h
#pragma once

#include <iostream>

template<typename T> class Complex { private: T real_part; T imaginary_part; public: Complex(T r = T(0), T i = T(0));

Complex operator+(const Complex &amp;other) const;

Complex operator-(const Complex &amp;other) const;

Complex operator*(const Complex &amp;other) const;

Complex operator/(const Complex &amp;other) const;

Complex operator^(double power) const;

friend std::ostream &amp;operator&lt;&lt;(std::ostream &amp;os, const Complex &amp;obj);

bool operator&lt;(const Complex &amp;other) const;

bool operator&gt;(const Complex &amp;other) const;

bool operator==(const Complex &amp;other) const;

bool operator!=(const Complex &amp;other) const;

bool operator&lt;(T other) const;

bool operator&gt;(T other) const;

bool operator==(T other) const;

bool operator!=(T other) const;

T get_length() const;

T get_real_part() const;

T get_imaginary_part() const;

void set_real_part(T r);

void set_imaginary_part(T i);

};

//complex_class.cpp
#include "complex_class.h"
#include <cmath>


template<typename T>
Complex<T>::Complex(T r, T i) : real_part(r), imaginary_part(i) {}

template<typename T>
Complex<T> Complex<T>::operator+(const Complex &other) const {
    Complex result;
    result.real_part = this->real_part + other.real_part;
    result.imaginary_part = this->imaginary_part + other.imaginary_part;
    return result;
}

template<typename T>
Complex<T> Complex<T>::operator-(const Complex &other) const {
    Complex result;
    result.real_part = this->real_part - other.real_part;
    result.imaginary_part = this->imaginary_part - other.imaginary_part;
    return result;
}

template<typename T>
Complex<T> Complex<T>::operator*(const Complex &other) const {
    Complex result;
    result.real_part = this->real_part * other.real_part - this->imaginary_part * other.imaginary_part;
    result.imaginary_part = this->real_part * other.imaginary_part + other.real_part * this->imaginary_part;
    return result;
}

template<typename T>
Complex<T> Complex<T>::operator/(const Complex &other) const {
    const T eps = T(1e-6);

    T divisor = (other.real_part * other.real_part) + (other.imaginary_part * other.imaginary_part);

    if (std::abs(divisor) < eps) {
        throw std::runtime_error("You can't divide by 0");
    }

    T newReal = ((real_part * other.real_part) + (imaginary_part * other.imaginary_part)) / divisor;
    T newImaginary = ((imaginary_part * other.real_part) - (real_part * other.imaginary_part)) / divisor;
    return Complex<T>(newReal, newImaginary);
}

template<typename T>
Complex<T> Complex<T>::operator^(double power) const {
    double magnitude = std::pow(std::sqrt(real_part * real_part + imaginary_part * imaginary_part), power);
    double angle = std::atan2(imaginary_part, real_part) * power;
    return Complex(magnitude * std::cos(angle), magnitude * std::sin(angle));
}

template<typename T>
std::ostream &operator<<(std::ostream &os, const Complex<T> &obj) {
    os << "Complex(" << obj.real_part << "f, " << obj.imaginary_part << "f)";
    return os;
}

template<typename T>
bool Complex<T>::operator<(const Complex &other) const {
    return this->get_length() < other.get_length();
}

template<typename T>
bool Complex<T>::operator>(const Complex &other) const {
    return this->get_length() > other.get_length();
}

template<typename T>
bool Complex<T>::operator==(const Complex &other) const {
    return this->get_length() == other.get_length();
}

template<typename T>
bool Complex<T>::operator!=(const Complex &other) const {
    return this->get_length() != other.get_length();
}

template<typename T>
bool Complex<T>::operator<(T other) const {
    return this->get_length() < other;
}

template<typename T>
bool Complex<T>::operator>(T other) const {
    return this->get_length() > other;
}

template<typename T>
bool Complex<T>::operator==(T other) const {
    return this->get_length() == other;
}

template<typename T>
bool Complex<T>::operator!=(T other) const {
    return this->get_length() != other;
}

template<typename T>
T Complex<T>::get_length() const {
    return sqrt(pow(this->get_real_part(), 2) + pow(this->get_imaginary_part(), 2));
}

template<typename T>
T Complex<T>::get_real_part() const {
    return real_part;
}

template<typename T>
T Complex<T>::get_imaginary_part() const {
    return imaginary_part;
}

template<typename T>
void Complex<T>::set_real_part(T r) {
    this->real_part = r;
}

template<typename T>
void Complex<T>::set_imaginary_part(T i) {
    this->imaginary_part = i;
}
//main.cpp
#include "complex_class.h"

int main() {
    using namespace std;

    Complex<float> first(1.1f, 1.2f);
    Complex<float> second(1.0f, 5.0f);

    cout << first + second;
}
Artem
  • 3

0 Answers0