c++ - 在 C++ 中编译复数类时出错

标签 c++ compiler-errors complex-numbers

我的代码应该显示复数的加法、减法等,无需用户输入。我有三个类:test.cpp、complex.cpp 和 complex.h 来分别运行程序、定义构造函数和方法以及创建头类。但是,当我运行我的代码时,出现了一系列错误,我已经尝试了一段时间了。

复杂.h

//complex class definition
#ifndef COMPLEX_H
#define COMPLEX_H


//class complex
class Complex
{
public:
    Complex(); //default no arg constructor
    Complex(double a); //one arg constructor
    Complex(double a, double b); //two arg constructor
    Complex operator+(const Complex &) const; //addition method
    Complex operator-(const Complex &) const; //subtraction method
    Complex operator*(const Complex &) const; //multiplication method
    Complex operator/(const Complex &) const; //division method
    void print() const; //output
private:
    double a; //real number
    double b; //imaginary number
}; //end class Complex

#endif

复杂.cpp

#include "stdafx.h"
#include <iostream>
#include "complex.h"
using namespace std;

//no arg constructor
Complex::Complex()
{
    a = 0;
    b = 0;
}

//one arg instructor
Complex::Complex(double real)
{
    a = real;
    b = 0;
}

//two arg constructor
Complex::Complex(double real, double imaginary)
{
    a = real;
    b = imaginary;
}

//addition
Complex Complex::operator+(const Complex &number2) const
{
    return a + number2.a, b + number2.b;
}

//subtraction
Complex Complex::operator-(const Complex &number2) const
{
    return a - number2.a, b - number2.b;
}

//multiplication
Complex Complex::operator*(const Complex &number2) const
{
    return a * number2.a, b * number2.b;
}

//division
Complex Complex::operator/(const Complex &number2) const
{
    return a / number2.a, b / number2.b;
}

//output display for complex number
void Complex::print() const
{
    cout << '(' << a << ", " << b << ')';
}

测试.cpp

#include <iostream>
#include <complex>
#include "complex.h"
#include "stdafx.h"
using namespace std;

int main()
{
    Complex b(1.0, 0.0);
    Complex c(3.0, -1.0);

    /*cout << "a: ";
    a.print();

    system ("PAUSE");*/

};

现在正在测试中,因为代码显示较低的部分被注释掉了,我试图只调用三个构造函数中的两个,看看我是否可以让其中的任何一个工作。

我收到的错误:

    error C2065: 'Complex' : undeclared identifier
    error C2065: 'Complex' : undeclared identifier
    error C2146: syntax error : missing ';' before identifier 'b'
    error C2146: syntax error : missing ';' before identifier 'c'
    error C3861: 'b': identifier not found  
    error C3861: 'c': identifier not found

我正在尝试在 Visual Studio 2010 中运行它。有人可以帮忙吗?

最佳答案

我认为问题出在您的 Complex.cpp 中。如果编译该类的代码时出现错误,则该类是未知的,因此标识符 Complex 也是未知的。

快速查看后,一切似乎都是正确的,但您的运算符函数有一些错误:

不正确:

    //addition
    Complex Complex::operator+(const Complex &number2) const
    {
        return a + number2.a, b + number2.b;
    }

正确:

    //addition
    Complex Complex::operator+(const Complex &number2) const
    {          
        return Complex(a + number2.a, b + number2.b);
    }

问题是您的运算符方法的返回类型。它们必须与声明的相同。您也不能同时返回两个变量。如果你想这样做,你必须使用 structs或类(通过使用新值调用 Complex 类的构造函数)。

如果要操作成员 a 和 b,则不能将方法定义为“const”。如果你说一个函数是 const,这意味着所有类成员的值都不会通过调用这个方法来操作。

关于c++ - 在 C++ 中编译复数类时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13357834/

相关文章:

c++ - 如何正确使用 std::shared_from_this 或绕过它?

c++ - 在类中使用析构函数

gcc - 无法识别的命令行选项 '--exclude-libs=libpthread.a' openblas mingw-w64

matlab - 将分子和分母多项式分解为偶数和奇数部分

c++ - 加载 OBJ 时我得到这个

c++ - 虚拟析构函数如何工作?

c++ - C++使用代码生成错误

c++ - 如何正确编译 header c++文件管理器

python - 在复平面上绘制图形

python - 获取复数的相位