Hello,

when i try to compile this program (it has three parts) I receive an error message that confuses me. Below are the code and error message.

#include <iostream>

using std::cout;
using std::endl;

#include "Complex.h"

int main()
{
	Complex x(2, 3), y(4, 5), z;

	x.printComplex();
	cout << " + ";
	y.printComplex();
	cout << " = ";
	x.add(y);
	x.printComplex();

	cout << '\n';
	x.setComplexNumber(10, 1); //reset Real
	y.setComplexNumber(11, 5); //reset Imaginery
	x.printComplex();
	cout << " - ";
	y.printComplex();
	cout << " = ";
	x.subtract(y);
	x.printComplex();

	cout << '\n';
	x.equal(y);
	cout << endl;

	return 0;
}
#ifndef Complex_H
#define Complex_H

class Complex {
public:
	Complex(double = 0.0, double = 0.0); //default constructor
	Complex Complex::add(Complex &);
	Complex Complex::subtract(Complex &);
	void printComplex(void);
	void setComplexNumber(double, double);
	Complex Complex::equal(Complex &x);
private:
	double realPart;
	double imagineryPart;
}

#endif
#include <iostream>

using std::cout;

#include <Complex.h>

Complex::Complex(double real, double imaginery)
{ setComplexNumber(real, imaginery); }

void Complex::add(Complex &)
{
	realPart += a.realPart;
	imagineryPart =+ a.imagineryPart;
}

void Complex::subtract(Complex &)
{
	realPart -= s.realPart;
	imagineryPart -= s.realPart;
}

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

void setComplexNumber(double rp, dounle ip)
{
	realPart = rp;
	imagineryPart = ip;
}

bool Complex::equal(Complex &x) {
	return ((realPart == x.realPart) && 
		(imagineryPart == x.imagineryPart));
}

The error msg says:
C:\IS20\Complex Classes\Complex.cpp(16) : error C2556: 'void __thiscall Complex::add(class Complex &)' : overloaded function differs only by return type from 'class Complex __thiscall Complex::add(class Complex &)'
c:\is20\complex classes\complex.h(10) : see declaration of 'add'
C:\IS20\Complex Classes\Complex.cpp(16) : error C2371: 'add' : redefinition; different basic types
c:\is20\complex classes\complex.h(10) : see declaration of 'add'
C:\IS20\Complex Classes\Complex.cpp(17) : error C2065: 'a' : undeclared identifier
C:\IS20\Complex Classes\Complex.cpp(17) : error C2228: left of '.realPart' must have class/struct/union type
C:\IS20\Complex Classes\Complex.cpp(18) : error C2228: left of '.imagineryPart' must have class/struct/union type
C:\IS20\Complex Classes\Complex.cpp(22) : error C2556: 'void __thiscall Complex::subtract(class Complex &)' : overloaded function differs only by return type from 'class Complex __thiscall Complex::subtract(class Complex &)'
c:\is20\complex classes\complex.h(11) : see declaration of 'subtract'
C:\IS20\Complex Classes\Complex.cpp(22) : error C2371: 'subtract' : redefinition; different basic types
c:\is20\complex classes\complex.h(11) : see declaration of 'subtract'
C:\IS20\Complex Classes\Complex.cpp(23) : error C2065: 's' : undeclared identifier
C:\IS20\Complex Classes\Complex.cpp(23) : error C2228: left of '.realPart' must have class/struct/union type
C:\IS20\Complex Classes\Complex.cpp(24) : error C2228: left of '.realPart' must have class/struct/union type
C:\IS20\Complex Classes\Complex.cpp(30) : error C2061: syntax error : identifier 'dounle'
C:\IS20\Complex Classes\Complex.cpp(33) : error C2065: 'ip' : undeclared identifier
C:\IS20\Complex Classes\Complex.cpp(36) : error C2556: 'bool __thiscall Complex::equal(class Complex &)' : overloaded function differs only by return type from 'class Complex __thiscall Complex::equal(class Complex &)'
c:\is20\complex classes\complex.h(14) : see declaration of 'equal'
C:\IS20\Complex Classes\Complex.cpp(36) : error C2371: 'equal' : redefinition; different basic types
c:\is20\complex classes\complex.h(14) : see declaration of 'equal'


I do not understand what that means. Can anyone help me? Thanks so much, I really appreciate it a lot.

Hi, thanks for reading my message. I've changed my code a bit and I get it to compile but the math is wrong.

#include <iostream>

using std::cout;
using std::endl;

#include "Complex.h"

int main()
{
	Complex x;
	Complex y( 3, 4);
	Complex z( 6, 2);

	cout << "x: ";
	x.printComplex();
	cout << "\ny: ";
	y.printComplex();
	cout << "\nz: ";
	z.printComplex();

	x = y + z;
	cout << "\n\nx = y + z:\n";
	x.printComplex();
	cout << " = ";
	y.printComplex();
	cout << " + ";
	z.printComplex();

	x = y - z;
	cout << "\n\nx = y - z:\n";
	x.printComplex();
	cout << " = ";
	y.printComplex();
	cout << " - ";
	z.printComplex();
	cout << endl;


	return 0;
}// end main
#include <iostream>

using std::cout;

#include "Complex.h"

Complex::Complex(double realPart, double imagineryPart)
: real( realPart ),
	imaginery( imagineryPart)
{
	//empty body
}

//addition
Complex Complex::operator+( Complex &operand2) 
{
	 return Complex( real + operand2.realPart, 
		imaginery + operand2.imagineryPart );
}//end

//subtraction
Complex Complex::operator-( Complex &operand2) 
{
	return Complex( real - operand2.realPart,
		imaginery - operand2.imagineryPart );
}//end

//display in form (a, b)
void Complex::printComplex() const
{ 
	cout << '(' << realPart << ", " << imagineryPart << ')'; 
}

//equal or not
bool Complex::equal(Complex &x) {
	return ((realPart == x.realPart) && 
		(imagineryPart == x.imagineryPart));
}
#ifndef Complex_H
#define Complex_H

class Complex {
public:
	Complex(double = 0.0, double = 0.0); //default constructor
	Complex Complex::operator+( Complex &);
	Complex Complex::operator-( Complex &);
	void printComplex() const;
	bool Complex::equal(Complex &x);
private:
	double realPart;
	double imagineryPart;
	double real;
	double imaginery;
};

#endif

Could you please look this over and tell me what myself and the debugger are messing up?

Also, how do I make use of the equal function so that it doesn't just sit there?

Thanks for your help.

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.