我正在尝试创建一个处理复数数组的程序,我的程序有两个类,一个代表复数,另一个代表一个数组。
这是我到目前为止创建的代码:
Complex.h
#pragma once
#include <functional>
class Complex
{
friend class Array;
double re, im;
friend std::ostream& operator<<(std::ostream&, const Array&);
friend std::istream& operator>>(std::istream&, Array&);
public:
double getRe() const;
double getIm() const;
void setRe(double);
void setIm(double);
Complex operator[](int);
Complex operator=(const Complex&);
Complex operator+(const Complex&);
Complex operator-(const Complex&);
private:
Complex apply(const Complex&, const std::function <double(double, double)> &);
};
Array.h
#pragma once
#include "Complex.h"
#include <functional>
class Array
{
Complex *arr;
int n;
friend std::ostream& operator<<(std::ostream&, const Array&);
friend std::istream& operator>>(std::istream&, Array&);
public:
Array();
Array(int);
Array(const Array&);
Array(Array&&);
~Array();
Array& operator=(const Array&);
Array& operator=(Array &&);
Array operator+(const Array&);
// Array operator-(const Array&);
private:
Array& copy(Array&);
Array& move(Array&);
Array compute(const Array &,const std::function<Complex(Complex, Complex)> &f);
};
Complex.cpp
#include <iostream>
#include "Complex.h"
double Complex::getRe() const
{
return re;
}
double Complex::getIm() const
{
return im;
}
void Complex::setRe(double re)
{
this->re = re;
}
void Complex::setIm(double im)
{
this->im = im;
}
Complex Complex::operator[](int i)
{
Complex temp;
temp.re = re;
temp.im = im;
return temp;
}
Complex Complex::operator=(const Complex &c)
{
re = c.re;
im = c.im;
return *this;
}
Complex Complex::operator+(const Complex &c)
{
*this = apply(c, [](double x, double y) { return x + y; });
return *this;
}
Complex Complex::operator-(const Complex &c)
{
*this = apply(c, [](double x, double y) { return x - y; });
return *this;
}
Complex Complex::apply(const Complex &c, const std::function<double(double, double)> &f)
{
re = re + c.re;
im = im + c.im;
return *this;
}
Array.cpp
Array::Array(int n)
{
this->n = n;
arr = new Complex[n];
}
Array::Array(const Array &a)
{
this->n = a.n;
arr = new Complex[n];
arr = a.arr;
}
Array::Array(Array &&a)
{
this->n = a.n;
arr = a.arr;
a.arr = nullptr;
}
Array& Array::operator=(const Array &a)
{
this->n = a.n;
arr = new Complex[n];
arr = a.arr;
return *this;
}
Array& Array::operator=(Array &&a)
{
this->n = n;
this->arr = a.arr;
a.arr = nullptr;
return *this;
}
std::ostream& operator<<(std::ostream& out, const Array &a)
{
for (int i = 0; i < a.n; i++)
out << a.arr[i].re << " +i" << a.arr[i].im;
return out;
}
std::istream& operator>>(std::istream& in, Array &a)
{
for (int i = 0; i < a.n; i++)
in >> a.arr[i].re >> a.arr[i].im;
return in;
}
Array Array::operator+(const Array &a)
{
return compute(a, [](Complex x, Complex y) { return x + y; });
}
/*
Array Array::operator-(const Array &a)
{
return compute(a, [](Complex x, Complex y) { return x - y; });
}
*/
Array Array::compute(const Array &a,const std::function<Complex(Complex , Complex)> &f)
{
for (int i = 0; i < n; i++)
arr[i] = f(arr[i], a.arr[i]);
return *this;
}
Array::~Array()
{
delete[] arr;
n = 0;
}
Source.cpp
#include <iostream>
#include "Array.h"
#include "Complex.h"
int main()
{
Array a(2);
std::cin >> a;
Array b;
b = a;
Array c(2);
c = a + b;
std::cout << c;
getchar();
getchar();
}
所以这就是我目前所拥有的,在此之前,我还需要补充一点,在我的Complex
类中,我只有二传手,getter,这些字段和Array
类一个朋友,这意味着我正在“手动”建立索引并操作+
和-
,而没有重载Complex
类中的相应运算符。
但是,我有一个想法,那就是最好(在oop的精神上)重载Complex
类所需的运算符,因为代码更具可读性。
但是,即使我得到了所需的结果,每次执行程序后,当我按Enter键时,都会出现析构函数错误,我尝试了一些调试,在我看来,当对象Array b
出现问题是“破坏”的,但我无法弄清楚实际发生了什么(而b首先确实是造成问题的原因)。
注意:我在这里发现了一些与复杂数组有关的问题,但是没有一个问题解决了我的问题。
任何帮助表示赞赏!