复数简单错误

时间:2013-03-28 01:30:34

标签: c++ compilation compiler-errors

嘿伙计们我是c +的新手,每次编译我的代码时都会说我在一个很好的行上有一个errorc2413,但我显然有一个半冒号。每当我输入ex等内容时,通常会发生这种情况。 Complex.real。我想知道你是否可以帮助我?

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&gt; ------构建开始:项目:复数计算器,配置:调试Win32 ------   1 GT; Calculator.cpp   1&gt; c:\ users \ victor \ documents \ visual studio 2010 \ projects \ complex number calculator \ complex number calculator \ calculator.cpp(3):error C2143:syntax error:missing';'在'使用'之前   1&gt; c:\ users \ victor \ documents \ visual studio 2010 \ projects \ complex number calculator \ complex number calculator \ complex.h(57):error C2143:syntax error:missing';'在'。'之前   1&gt; c:\ users \ victor \ documents \ visual studio 2010 \ projects \ complex number calculator \ complex number calculator \ complex.h(57):error C2143:syntax error:missing';'在'。'之前   1&gt; c:\ users \ victor \ documents \ visual studio 2010 \ projects \ complex number calculator \ complex number calculator \ complex.h(58):error C2143:syntax error:missing';'在'。'之前   1&gt; c:\ users \ victor \ documents \ visual studio 2010 \ projects \ complex number calculator \ complex number calculator \ complex.h(58):error C2143:syntax error:missing';'在'。'之前   1&gt; c:\ users \ victor \ documents \ visual studio 2010 \ projects \ complex number calculator \ complex number calculator \ complex.h(64):error C2143:syntax error:missing';'在'。'之前   1&gt; c:\ users \ victor \ documents \ visual studio 2010 \ projects \ complex number calculator \ complex number calculator \ complex.h(64):error C2143:syntax error:missing';'在'。'之前   1&gt; c:\ users \ victor \ documents \ visual studio 2010 \ projects \ complex number calculator \ complex number calculator \ complex.h(65):error C2143:syntax error:missing';'在'。'之前   1&gt; c:\ users \ victor \ documents \ visual studio 2010 \ projects \ complex number calculator \ complex number calculator \ complex.h(65):error C2143:syntax error:missing';'在'。'之前   ==========构建:0成功,1个失败,0个最新,0个跳过==========

1 个答案:

答案 0 :(得分: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具有相同的问题。还建议在你的getter上使用const,例如:

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

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