我的代码应该显示复数的加法、减法等,而无需用户输入。我有三个类: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 中运行它。有人可以帮忙吗?