Hey i wrote this program but im getting some type of error in one of the lines of my code. This program is suppose to add, subtract, and print the complex numbers. I wont type out the whole assignment but this is basically what it does. I'll really appreciate it if someone can help me with this program. My code is at the bottom

#include <iostream>
#include <iomanip>
using namespace std;

// class declaration
class Complex {
  public:
    Complex( double = 0.0, double = 0.0 ); // default constructor
    Complex addition( const Complex & );
    Complex subtraction( const Complex & );
    void printComplex( void );
    void setComplexNumber( double, double );
  private:
    double realPart;
    double imaginaryPart;
};

// member functions definition
Complex::Complex( double real, double imaginary )
   { setComplexNumber( real, imaginary ); }

Complex Complex::addition( const Complex &a )
{
    Complex t;

    t.realPart = realPart + a.realPart;
    t.imaginaryPart = imaginaryPart + a.imaginaryPart;

    return t;
}

Complex Complex::subtraction( const Complex &s )
{
    Complex t(realPart - a.realPart, imaginaryPart - a.imaginaryPart);

    return t;
}

void Complex::printComplex( void )
   { cout << '(' << realPart << ", " << imaginaryPart << ')'; }

void Complex::setComplexNumber( double rp, double ip )
{
    realPart = rp;
    imaginaryPart = ip;
}

//driver program
int main()
{
    Complex b( 1, 7 ), c( 9, 2 );
    Complex a;

    b.printComplex();
    cout << " + ";
    c.printComplex();
    cout << " = ";
    a = b.addition(c);
    a.printComplex();
    cout << endl;

    b.setComplexNumber( 10, 1 );   // reset realPart and imaginaryPart
    c.setComplexNumber( 11, 5 );

    b.printComplex();
    cout << " - ";
    c.printComplex();
    cout << " = ";
    a = b.subtraction(c);
    a.printComplex();
    cout << endl;

    return 0;
}

>This program is suppose to add, subtract, and print the complex numbers.
It'd be 100% easier if you used the standard complex stuff from the <complex> header. Despite the name, it's actually quite easy. ;)

>but im getting some type of error in one of the lines of my code.
Is the error something like (Error: "a" is an unrecognized identifier)? And the line number takes you to this member function?

Complex Complex::subtraction( const Complex &s )
{
    Complex t(realPart - a.realPart, imaginaryPart - a.imaginaryPart);
    
    return t;
}

If so, you probably meant to use s instead of a. Beware the evils of copy/paste programming. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.