Hi there, I'm trying to create a complex number class in c++ and have hit a snag. I have searched the forums here for an answer and have tried to google it, but still wasnt able to figure out what is wrong. I am fairly new to C++ so yeah. Here is the code so far, I am having troubles with overloading the == and testing it.

complex.h

#ifndef COMPLEX_H
#define COMPLEX_H

#include <iostream>

using namespace std;

class Complex
{
	// Data Members
private:
		double real;	 // x and y are real numbers 
		double imag; // the form of the complex number is c = x + iy and i^2 = -1
	
	// Methods
public:
  	// Constructors
   	Complex() {real = 0; imag = 1;};
  	// Default constructor
  	// Post: An instance of Complex has been created and initialized to (0 + i0 = 0).
  	
  	Complex(const Complex& c);
		// Copy Constructor
		// Post: An instance of fraction has been created and initialized with an 
		//		exact copy of 'f'.
		// Note: This provides the mechanism for the assignment operation and for 
		//		passing of parameters.
		
	Complex(double x, double y ) {real = x; imag = y;};
		// Complex Number Constructor
		// Post: An instance of complex has been created that has the same value  
		//		as double x and double y.
	
		// Mutators
	double getReal() {return real;};
  	// Post: The x from the calling instance has been returned.
  	
  	double getImag() {return imag;};
  	// Post: The y from the calling instance has been returned.
  	
  	void setReal(double x) {real = x;};
  	// Pre: 'x' is a double;
  	// Post: The x from the calling instance has been set to 'x'.
  	
  	void setImag(double y) {imag = y;};
  	// Pre: 'y' is a double;
  	// Post: The y from the calling instance has been set to 'y'.

	double getNorm();
	//Pre: a real and imaginary number must be given.
	//Post: the norm "sqrt(real^2 + imaginary^2)" is returned.

	bool operator == (Complex& check);
	//Post The boolean value of "this == check" has been returned.

	//bool operator > (const Complex& b);

complex.cpp

#include "complex.h"
#include <cmath>
// Implementation of the member functions

Complex::Complex(const Complex& c)
{
	real = c.real;
	imag = c.imag;
}
double Complex::getNorm()
{
	// Find the Greatest Common Divisor of the numerator and denominator
	double x = real;
	double y = imag;
	double norm = (x * x) + (y * y);
	norm = sqrt(norm);
	return norm;
	
}
bool Complex::operator == (Complex& check)
{
	if ((real == check.getReal) && (imag == check.getImag))
		return true;
	else
		return false;
}

complexTest.cpp

#include "complex.h"
#include <cmath>
// Implementation of the member functions

Complex::Complex(const Complex& c)
{
	real = c.real;
	imag = c.imag;
}
double Complex::getNorm()
{
	// Find the Greatest Common Divisor of the numerator and denominator
	double x = real;
	double y = imag;
	double norm = (x * x) + (y * y);
	norm = sqrt(norm);
	return norm;
	
}
bool Complex::operator == (Complex& check)
{
	if ((real == check.getReal) && (imag == check.getImag))
		return true;
	else
		return false;
}

when I run the make file I get this error

omplex.cpp: In member function ‘bool Complex::operator==(Complex&)’:
complex.cpp:33:21: error: invalid use of member (did you forget the ‘&’ ?)
complex.cpp:33:48: error: invalid use of member (did you forget the ‘&’ ?)
make: *** [complex.o] Error 1

I found the error. you called check.getReal() and check.getImag() without the (). That's why your compiler is grumpy.

Ok silly mistake, but thanks for the heads up. However, now I'm getting an error


complex.cpp: In member function ‘bool Complex::operator==(const Complex&)’:
complex.cpp:33:29: error: passing ‘const Complex’ as ‘this’ argument of ‘double Complex::getReal()’ discards qualifiers
complex.cpp:33:58: error: passing ‘const Complex’ as ‘this’ argument of ‘double Complex::getImag()’ discards qualifiers
make: *** [complex.o] Error 1

which I dont quite understand and dont know how to fix

You just told the compiler that you promise not to modify whatever you're passing in. However, even though we know getReal() and getImag() don't modify the Complex object, the compiler doesn't. That's why it's mad now. It thinks you're trying to change something that isn't supposed to change.
How to fix: (in complex.h)
double getReal(){return real;}; should read
double getReal() const {return real;}
by doing this, you're promising that getReal() does not change the Complex object it is applied to.
likewise with getImag()

By the way, when you define the member function in the class defenition (like you did with getReal() and getImag(), you don't have to follow with a ";".

It's like the difference between

int add(int a, int b); <--

//main code

int add(int a, int b)
{
return a+b;
} <--

This article has been dead for over six months. Start a new discussion instead.