Member Avatar for BobTheLob

Hey, so i've looked around online for a bit, and after finding similar, but not exact (as in it doesn't help me), i'm gonna post my problem here :P.
I'm working on a class to handle Complex numbers. This is going to require a lot of operator overloads and the sort to handle the different operations i need to test. Well one of the things that i need to do is be able to use the << and >> operators to input/output data so things are read/shown properly. My header file is below:

// ********************************************************
	// Complex.h
	//
	// Summary: This file is used to declare the Complex class.
	//					
	// Author: Andrew Stoker & Gregory Devos
	// Created: Feb 2011
	// Summary of Modifications:
	//	
	//  ©Copyright Cedarville University, its Computer Science faculty, and the 
	//  authors.  All rights reserved.
	//  *******************************************************


#ifndef COMPLEX_H
#define COMPLEX_H

class Complex {
	
public:
	
	Complex(); 
	
	Complex(double rNum); 
	
	Complex(double rNum, double iNum);
	
		//Facilitators
	Complex add(const Complex &c, const Complex &d) const;
	
	Complex subtract(const Complex &c, const Complex &d) const;
	
	Complex multiply(const Complex &c, const Complex &d) const;
	
	Complex divide(const Complex &c, const Complex &d) const;
	
	Complex equal(const Complex &c, const Complex &d) const;
	
	Complex gT(const Complex &c, const Complex &d) const;  //Greater Than
	
	Complex lT(const Complex &c, const Complex &d) const;  //Less Than
	
	Complex gTE(const Complex &c, const Complex &d) const; //Greater Than Equal
	
	Complex lTE(const Complex &c, const Complex &d) const; //Less Than Equal 
	

	
	
protected:
		//setters & getters
		//void setComplex(double realNum, double imagNum);
	
		//Complex getComplex(double realNum, double imagNum);
	
	
private:
	
	double realVal;
	double imagVal;
	
};

Complex operator+ (const Complex &r, const Complex &s);
Complex operator- (const Complex &r, const Complex &s);
Complex operator* (const Complex &r, const Complex &s);
Complex operator/ (const Complex &r, const Complex &s);
Complex operator== (const Complex &r, const Complex &s);
	//Complex operator= (const Complex &r);
Complex operator!= (const Complex &r, const Complex &s);
Complex operator< (const Complex &r, const Complex &s);
Complex operator> (const Complex &r, const Complex &s);
Complex operator<= (const Complex &r, const Complex &s);
Complex operator>= (const Complex &r, const Complex &s);



ostream& operator<< (ostream &sout, const Complex &r);
istream& operator>> (istream &sin, Complex &r);


#endif

Now, the error i'm getting is, "Expected constructor, destructor, or type conversion before '&' token. And i'm getting this error for these two lines of code near the end of the program:

ostream& operator<< (ostream &sout, const Complex &r);
istream& operator>> (istream &sin, Complex &r);

So the question is, how do i properly implement this operator overload so that inside the Complex.cpp file, i can handle the outputs properly?

Recommended Answers

All 4 Replies

They must be declared as friends inside the class's scope.Try that

Member Avatar for BobTheLob

They must be declared as friends inside the class's scope.Try that

Is there any way i can use it without declaring it as friend inside the class?

Not from what i know.

Member Avatar for BobTheLob

Not from what i know.

Well, it works. So i'll take it :P. Thanks man.

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.