Solved

Overloading operators outside of class

Posted on 2009-05-19
2
327 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

685 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