Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Overloading operators outside of class

Posted on 2009-05-19
2
Medium Priority
?
337 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
[X]
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
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses

664 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