0

嘿伙计们,我是 C++ 新手,每次我编译我的代码时,它总是说我在很多行上有一个 errorc2413,但我显然在那里有一个分号。每当我键入诸如 ex 之类的内容时,通常都会发生这种情况。复杂的。真实的。我想知道你能不能帮帮我?

using namespace std;
#include <iostream>

class Complex
{ 
private:
        double real;
      double imaginary;
public:
Complex()
{
    real = 0.0;
    imaginary = 0.0;
}
Complex(double r, double i)
{
    real = r;
    imaginary= i;
}

//Setters
void setReal(double r)
{
    real = r;
}
void setImaginary(double i)
{
    imaginary = i;
}

//Getters
double getReal()
{
    return real;
}// end getReal
double getImaginary()
{
    return imaginary;
}//end of getImaginary()

void output()
{
    cout<< real <<" + i"<< imaginary; 
}

Complex add(Complex a)
{
    Complex result;
    result.real= a.real + real;
    result.imaginary = a.imaginary+imaginary;
    return result;

}
Complex subtract(Complex a)
{
    Complex result;
    Complex.real = (real - a.real);
    Complex.imaginary = (imaginary - a.imaginary);
    return result;
}
Complex mul(Complex a)
{
    Complex result;
    Complex.real = (real*a.real)-(imaginary*a.imaginary);
    Complex.imaginary = (real*a.imaginary)+(imaginary*a.real);
    return result;
}
Complex div(Complex a)
{
    Complex result;
    result.real = 
    ((real*a.real)+(imaginary*a.imaginary))/((a.real*a.real)+(a.imaginary+a.imaginary));
    result.imaginary = 
        ((imaginary*a.real)-(real*a.imaginary))/((a.real*a.real)+(a.imaginary+a.imaginary));
    return result;
}
}//end complex class

1>----- 构建开始:项目:复数计算器,配置:调试 Win32 ------ 1> Calculator.cpp 1>c:\users\victor\documents\visual studio 2010\projects\complex数字计算器\复数计算器\calculator.cpp(3):错误 C2143:语法错误:缺少“;” 在“使用”之前 1>c:\users\victor\documents\visual studio 2010\projects\复数计算器\复数计算器\complex.h(57):错误 C2143:语法错误:缺少“;” 前 '。' 1>c:\users\victor\documents\visual studio 2010\projects\复数计算器\复数计算器\complex.h(57):错误C2143:语法错误:缺少';' 前 '。' 1>c:\users\victor\documents\visual studio 2010\projects\复数计算器\复数计算器\complex.h(58):错误C2143:语法错误:缺少 ';' 前 '。' 1>c:\users\victor\documents\visual studio 2010\projects\复数计算器\复数计算器\complex.h(58):错误C2143:语法错误:缺少';' 前 '。' 1>c:\users\victor\documents\visual studio 2010\projects\复数计算器\复数计算器\complex.h(64):错误C2143:语法错误:缺少';' 前 '。' 1>c:\users\victor\documents\visual studio 2010\projects\复数计算器\复数计算器\complex.h(64):错误C2143:语法错误:缺少';' 前 '。' 1>c:\users\victor\documents\visual studio 2010\projects\复数计算器\复数计算器\complex.h(65): 错误 C2143: 语法错误: 缺少';' 前 '。' 1>c: \users\victor\documents\visual studio 2010\projects\复数计算器\复数计算器\complex.h(65):错误C2143:语法错误:缺少';' 前 '。' ========== 构建:0 成功,1 失败,0 最新,0 跳过 ==========

4

1 回答 1

6

所以有几个错误,一个类定义的结尾需要一个;. 它看起来也像你在几个地方Complex代替了result,例如:

Complex mul(Complex a)
{
    Complex result;
    Complex.real = (real*a.real)-(imaginary*a.imaginary);
    Complex.imaginary = (real*a.imaginary)+(imaginary*a.real);
    return result;
}

看起来应该是:

Complex mul(Complex a)
{
    Complex result;
    result.real = (real*a.real)-(imaginary*a.imaginary);
    result.imaginary = (real*a.imaginary)+(imaginary*a.real);
    return result;
}

一旦我解决了上述问题,它看起来就像编译得很好。该函数subtract也有与mul. 还建议const在您的吸气剂上使用,例如:

double getReal() const
{                ^^^^^
    return real;
}

对于您知道永远不应更改任何对象变量的方法,这是一种很好的做法。

于 2013-03-28T01:34:58.833 回答