Solved

Overloading operators outside of class

Posted on 2009-05-19
2
311 Views
Last Modified: 2013-12-14
Experts,
 I am trying to write implementation files for my class headers and in these implementation files, I want to overload certain operators. I am getting errors like:
 '<' : is not a member of 'C_Aircaft AND:
 '==' : modifiers not allowed on nonmember functions      

Below is a copy of the class declarator and the relevant section of the implementation file - any clues?

Thanks



======================CLASS DECLARATOR======================

friend bool operator < (const C_Aircraft& a1, const C_Aircraft& a2)const;	// Operator overload declarator

'===================IMPLEMENTATION CPP FILE========================

bool C_Aircraft::operator < (const C_Aircraft& a1, const C_Aircraft& a2)const

{

                                         if(a1.model==a2.model)

		return(a1.callsign<a2.callsign)?true:false;

		return (a1.model<a2.model)?true:false;

}

Open in new window

0
Comment
Question by:simondopickup
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
Comment Utility
A non- member operator cannot be declared as 'const' (at least that makes no sense). Also, the global operator cannot be defined at class scope. So, try
// foward declaration

 

operator < (const C_Aircraft& a1, const C_Aircraft& a2);

 

class C_Aircraft

{

 

friend bool operator < (const C_Aircraft& a1, const C_Aircraft& a2);       // Operator overload declarator

 

// rest of declaration

 

};

 

// implementation

bool operator < (const C_Aircraft& a1, const C_Aircraft& a2)

{

         if(a1.model==a2.model)

                return(a1.callsign<a2.callsign)?true:false;

 

         return (a1.model<a2.model)?true:false;

}

Open in new window

0
 

Author Closing Comment

by:simondopickup
Comment Utility
Excellent, thanks JKR
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now