victor tr
victor tr

Reputation: 61

Complex number simple error

Hey guys I'm new to c++ and every time I compile my code it keeps on saying I have an errorc2413 on a good amount of lines but I clearly have a semi colon there. This usually occurs whenever I type something such as ex. Complex.real. I was wondering if you can help me out?

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>------ Build started: Project: Complex Number Calculator, Configuration: Debug Win32 ------ 1> Calculator.cpp 1>c:\users\victor\documents\visual studio 2010\projects\complex number calculator\complex number calculator\calculator.cpp(3): error C2143: syntax error : missing ';' before 'using' 1>c:\users\victor\documents\visual studio 2010\projects\complex number calculator\complex number calculator\complex.h(57): error C2143: syntax error : missing ';' before '.' 1>c:\users\victor\documents\visual studio 2010\projects\complex number calculator\complex number calculator\complex.h(57): error C2143: syntax error : missing ';' before '.' 1>c:\users\victor\documents\visual studio 2010\projects\complex number calculator\complex number calculator\complex.h(58): error C2143: syntax error : missing ';' before '.' 1>c:\users\victor\documents\visual studio 2010\projects\complex number calculator\complex number calculator\complex.h(58): error C2143: syntax error : missing ';' before '.' 1>c:\users\victor\documents\visual studio 2010\projects\complex number calculator\complex number calculator\complex.h(64): error C2143: syntax error : missing ';' before '.' 1>c:\users\victor\documents\visual studio 2010\projects\complex number calculator\complex number calculator\complex.h(64): error C2143: syntax error : missing ';' before '.' 1>c:\users\victor\documents\visual studio 2010\projects\complex number calculator\complex number calculator\complex.h(65): error C2143: syntax error : missing ';' before '.' 1>c:\users\victor\documents\visual studio 2010\projects\complex number calculator\complex number calculator\complex.h(65): error C2143: syntax error : missing ';' before '.' ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Upvotes: 0

Views: 418

Answers (1)

Shafik Yaghmour
Shafik Yaghmour

Reputation: 158619

So there are a couple of errors, one the end of a class definition needs a ;. It also looks like you have Complex in place of result in several places, for example:

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

looks like it should be:

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

Once I fix the above issues it looks like it compiles just fine. The function subtract also has the same issue as mul. Also the suggestion was made to use const on your getters, for example:

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

This is good practice for methods you know should never alter any of the objects variables.

Upvotes: 6

Related Questions