Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Overloading operators outside of class

Posted on 2009-05-19
2
325 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
ID: 24423695
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
ID: 31583077
Excellent, thanks JKR
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

839 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