Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

I am working on an assignment for my c++ class where I am doing operator overloading. I have everything working but the = operator, I am not sure how to make it work. I need to be able to do something like Complex myNumber = 4.4 5.5 and have it store that. I have attached my code below.

I am being told too many identifiers on my = code... can you see aproblem?

#include <iostream>

using std::cout;

#include"complex1.h"// Complex class definition

// constructor

Complex::Complex( double realPart, double imaginaryPart )

: real( realPart ),

imaginary( imaginaryPart )

{

// empty body

} // end Complex constructor

// addition operator

Complex Complex::operator+( const Complex &operand2 ) const

{

return Complex( real + operand2.real,

imaginary + operand2.imaginary );

} // end function operator+

// subtraction operator

Complex Complex::operator-( const Complex &operand2 ) const

{

return Complex( real - operand2.real,

imaginary - operand2.imaginary );

} // end function operator-

// I used the algorhthym posted in our conference. I had something similar but my parens were off I think :)

Complex Complex::operator*( const Complex &operand2 ) const

{

return Complex( ((real * operand2.real)-(imaginary * operand2.imaginary)), ((real * operand2.imaginary)+(operand2.real * imaginary)) );

} // end function operator*

Complex Complex::operator=( const double realPart, const double imaginaryParft, const Complex& complexNumber ) const

{

complexNumber.getReal(realPar);

complexNumber.getImaginary(imaginaryPart);

return complexNumber

}

std::ostream& operator<<(std::ostream& stream,const Complex& complexNumber)

{

stream << "(" << complexNumber.getReal() << " , " << complexNumber.getImaginary()<<")" << std::endl;

return stream;

}

std::istream& operator>>(std::istream& stream,Complex& value)

{

double realPart, imaginaryPart;

stream >> realPart >> imaginaryPart;

value.getReal(realPart);

value.getImaginary(imaginaryPart);

return stream;

}

bool Complex::operator==( const Complex &operand2 ) const

{

if (real == operand2.real && imaginary == operand2.imaginary)

return true;

else

return false;

}

bool Complex::operator!=( const Complex &operand2 ) const

{

if (real != operand2.real || imaginary != operand2.imaginary)

return true;

else

return false;

}

-------------------------------

#ifndef COMPLEX1_H

#define COMPLEX1_H

class Complex {

public:

Complex( double = 0.0, double = 0.0 ); // constructor

Complex operator+( const Complex & ) const; // addition

Complex operator-( const Complex & ) const; // subtraction

Complex operator*( const Complex &operand2 ) const; // Multiplication

Complex operator=( const double realPart, const double imaginaryParft, const Complex& complexNumber );

bool operator==( const Complex &operand2 ) const;

bool operator!=( const Complex &operand2 ) const;

friend std::ostream &operator<<(std::ostream &stream, const Complex &value);

friend std::istream &operator>>(std::istream &stream, Complex &value);

//accessor functions

double getReal() const { return real; }

double getImaginary() const { return imaginary; }

void getReal(double r) { real = r; }

void getImaginary(double i) { imaginary = i; }

private:

double real; // real part

double imaginary; // imaginary part

}; // end class Complex

#endif

I am being told too many identifiers on my = code... can you see aproblem?

#include <iostream>

using std::cout;

#include"complex1.h"// Complex class definition

// constructor

Complex::Complex( double realPart, double imaginaryPart )

: real( realPart ),

imaginary( imaginaryPart )

{

// empty body

} // end Complex constructor

// addition operator

Complex Complex::operator+( const Complex &operand2 ) const

{

return Complex( real + operand2.real,

imaginary + operand2.imaginary );

} // end function operator+

// subtraction operator

Complex Complex::operator-( const Complex &operand2 ) const

{

return Complex( real - operand2.real,

imaginary - operand2.imaginary );

} // end function operator-

// I used the algorhthym posted in our conference. I had something similar but my parens were off I think :)

Complex Complex::operator*( const Complex &operand2 ) const

{

return Complex( ((real * operand2.real)-(imaginary * operand2.imaginary)), ((real * operand2.imaginary)+(opera

} // end function operator*

Complex Complex::operator=( const double realPart, const double imaginaryParft, const Complex& complexNumber ) const

{

complexNumber.getReal(real

complexNumber.getImaginary

return complexNumber

}

std::ostream& operator<<(std::ostream& stream,const Complex& complexNumber)

{

stream << "(" << complexNumber.getReal() << " , " << complexNumber.getImaginary

return stream;

}

std::istream& operator>>(std::istream& stream,Complex& value)

{

double realPart, imaginaryPart;

stream >> realPart >> imaginaryPart;

value.getReal(realPart);

value.getImaginary(imagina

return stream;

}

bool Complex::operator==( const Complex &operand2 ) const

{

if (real == operand2.real && imaginary == operand2.imaginary)

return true;

else

return false;

}

bool Complex::operator!=( const Complex &operand2 ) const

{

if (real != operand2.real || imaginary != operand2.imaginary)

return true;

else

return false;

}

--------------------------

#ifndef COMPLEX1_H

#define COMPLEX1_H

class Complex {

public:

Complex( double = 0.0, double = 0.0 ); // constructor

Complex operator+( const Complex & ) const; // addition

Complex operator-( const Complex & ) const; // subtraction

Complex operator*( const Complex &operand2 ) const; // Multiplication

Complex operator=( const double realPart, const double imaginaryParft, const Complex& complexNumber );

bool operator==( const Complex &operand2 ) const;

bool operator!=( const Complex &operand2 ) const;

friend std::ostream &operator<<(std::ostream &stream, const Complex &value);

friend std::istream &operator>>(std::istream &stream, Complex &value);

//accessor functions

double getReal() const { return real; }

double getImaginary() const { return imaginary; }

void getReal(double r) { real = r; }

void getImaginary(double i) { imaginary = i; }

private:

double real; // real part

double imaginary; // imaginary part

}; // end class Complex

#endif

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

like Complex number1, number2;

number1=number2?

Is that possible?

I cannot provide a direct answer for your question because it is a homework, but you can learn from this similar example and adapt to your needs:

http://www.fredosaurus.com/notes-cpp/oop-overloading/overloadassign.html

complex1.cpp(48) : error C2440: '=' : cannot convert from 'const double *__w64 ' to 'double'

1> There is no context in which this conversion is possible

complex1.cpp(49) : error C2440: '=' : cannot convert from 'const double *__w64 ' to 'double'

1> There is no context in which this conversion is possible

Complex Complex::operator=(const Complex& operand2 )

{

if (this != &operand2)

{

real = &operand2.real;

imaginary=&operand2.imagin

}

return *this;

}

New Header file after change

#ifndef COMPLEX1_H

#define COMPLEX1_H

class Complex {

public:

Complex( double = 0.0, double = 0.0 ); // constructor

Complex operator+( const Complex & ) const; // addition

Complex operator-( const Complex & ) const; // subtraction

Complex operator*( const Complex &operand2 ) const; // Multiplication

Complex operator=( const Complex& operand2 );

bool operator==( const Complex &operand2 ) const;

bool operator!=( const Complex &operand2 ) const;

friend std::ostream &operator<<(std::ostream &stream, const Complex &value);

friend std::istream &operator>>(std::istream &stream, Complex &value);

//accessor functions

double getReal() const { return real; }

double getImaginary() const { return imaginary; }

void getReal(double r) { real = r; }

void getImaginary(double i) { imaginary = i; }

private:

double real; // real part

double imaginary; // imaginary part

}; // end class Complex

#endif

Complex Complex::operator=(const Complex& operand2 )

{

if (this != &operand2)

{

real = operand2.real; // removed ampersand

imaginary = operand2.imaginary; // removed ampersand

}

return *this;

}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Editors IDEs

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

You cannot do this. You can assign only one value (whatever type is it)

As a general rule, when you overload an operator you cannot modify the way it is used (precedence, association, etc).