我们被要求在main函数中创建两个实例并进行计算,但是我不知道,总是出现错误消息...
这是Complex.h

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <ctime>

class Complex
 {
   private:
   int real1,real2;
   double imag1,imag2;

   public:
   /*Complex();
   ~Complex(){  };*/
   void setReal(int newreal);/*setter function*/
   void setImag(double newimag);

   int getReal();/*getter function*/
   double getImag();

    void printcom(int real1, double imag1, int real2, double imag2);/*print the entered      values in mathematical form of complex number*/

   void Conjugate(int real1, double imag1, int real2, double imag2);/*calculations for complex number*/
   void Add(int real1, double imag1, int real2, double imag2);
   void Subtract(int real1, double imag1, int real2, double imag2);
   void Multiply(int real1, double imag1, int real2, double imag2);

 };


complex.cpp太长,因此我在这里不显示它,并且可以正常工作。

然后是主要的测试功能:testcomplex.cpp

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <ctime>
#include "Complex.h"

using namespace std;

int main()
{
    Complex a,z,c;
    int real;
    double imag;

    cout << "The real part of the first complex number: "<<endl;
    cin >> real;
    a.setReal(real);
    cout << "The imaginary part of the first complex number: "<<endl;
    cin >> imag;
    a.setImag(imag);
    cout << "The real part of the second complex number: "<<endl;
    cin >> real;
    z.setReal(real);
    cout << "The imaginary part of the second complex number: "<<endl;
    cin >> imag;
    z.setImag(imag);

    c.printcom(a.real1,a.imag1,z.real1,imag1);
    c.Conjugate(a.real1,a.imag1,z.real1,imag1);
    c.Add(a.real1,a.imag1,z.real1,imag1);
    c.Subtract(a.real1,a.imag1,z.real1,imag1);
    c.Multiply(a.real1,a.imag1,z.real1,imag1);

    return 0;
}

最佳答案

您的作业中清楚地写着“要求我们在主函数中创建两个实例并进行计算”

这意味着应该编写Complex类以仅定义一个复数。那么复数如何具有两个实部和虚部?
因此,代替

class Complex
 {
   private:
   int real1,real2;
   double imag1,imag2;


必须有

class Complex
 {
   private:
   int real;
   double imag;


我也不明白为什么类的实际部分是int类型,而imagenary是double类型。

此外,成员函数的声明不正确。例如函数printcom应该声明为

void printcom() const;


或函数Add应该声明为成员函数

const Complex Add( const Complex &rhs ) const;


作为非成员函数

const Complex Add( const Complex &lhs, const Complex &rhs );

10-06 13:07