简体   繁体   English

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

[英]Errors when compiling complex number class in c++

I have code that is supposed to show addition, subtraction, etc. of complex numbers with no input from the user necessary. 我的代码应该显示复杂数字的加法,减法等,而无需用户输入。 I have three classes: test.cpp, complex.cpp, and complex.h to run the program, define the constructors and methods, and create a header class respectively. 我有三个类:test.cpp,complex.cpp和complex.h,用于运行程序,定义构造函数和方法以及分别创建标头类。 However, when I run my code I get a series of errors that I've been trying to figure out for a while now. 但是,当我运行我的代码时,出现了一系列我一直想找出一堆错误。

complex.h complex.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

complex.cpp complex.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 << ')';
}

test.cpp TEST.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");*/

};

In test right now as the code shows the lower parts are commented out and I have attempted to only call two of the three constructors to see if I can get any of this working. 现在在测试中,如代码所示,下面的部分已被注释掉,我试图仅调用三个构造函数中的两个,以查看是否可以进行任何工作。

The errors I receive: 我收到的错误:

    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

I am trying to run this in Visual Studio 2010. Can someone please help? 我正在尝试在Visual Studio 2010中运行它。有人可以帮忙吗?

I think the problem is in your Complex.cpp. 我认为问题出在您的Complex.cpp中。 If there are errors compiling the code of the class, the class is unknown and so the identifier Complex, too. 如果编译该类的代码时出错,则该类是未知的,因此标识符也为Complex。

After a quick view, everthing seem to be correct but your operator functions have some errors: 快速浏览后,一切似乎都是正确的,但您的操作员函数有一些错误:

Incorrect: 不正确:

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

Correct: 正确:

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

The problems are the return types of your operator methods. 问题出在您的运算符方法的返回类型上。 They have to be the same as declared. 它们必须与声明的相同。 Also you cannot return two variables at the same time. 同样,您不能同时返回两个变量。 If you want to do so, you have to use structs or classes (by calling the constructor of your class Complex with the new values). 如果要这样做,则必须使用结构或类(通过使用新值调用类Complex的构造函数)。

If you want to manipulate the members a and b, you cannot define the methods as "const". 如果要操作成员a和b,则不能将方法定义为“ const”。 If you say a function is const, it means all class members values won't be manipulated by calling this method. 如果您说一个函数是const,则意味着不会通过调用此方法来操纵所有类成员的值。

You may facing a problem with stdafx.h issue 您可能会遇到stdafx.h问题

At least you should try to put #include "stdafx.h before every other include Or you could try to set your project property to not use stdafx.h 至少您应该尝试将#include "stdafx.h放在所有其他include之前,或者可以尝试将您的项目属性设置为不使用stdafx.h

1. right click on project and select Properties
2. go to C/C++ -> Precompiled Headers 
3. select "Not Using Precompiled Headers"
#include <complex>
#include "complex.h"

That's a bad smell. 那是难闻的气味。 It's possible that both those #include files use the same name for the header guard. 这两个#include文件都可能使用相同的标题防护名称。

Try changing yours like this. 尝试像这样改变你的。

#ifndef MY_COMPLEX_H
#define MY_COMPLEX_H

.. And maybe rename your module from "complex" to something else, to avoid clashes with the system header file of the same name. ..也许将模块从“复杂”重命名为其他名称,以避免与同名的系统头文件发生冲突。

Try making it as simple as possible. 尝试使其尽可能简单。 Then add parts back until you find your error. 然后重新添加零件,直到找到错误。

Does this work? 这样行吗?

#include "complex.h"

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

    return 0;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM