473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Overloaded global class functions

I recently created a simple class for a homework assignment and ran into compilation errors concerning overloaded global functions. I've copied the relevant parts of my code below and the errors generated on two compilers

Class Definition:

Expand|Select|Wrap|Line Numbers
  1. class BDay
  2.  
  3. {      //Should Be Global Scope
  4.       bool operator==( const BDay & , const BDay &);
  5.       bool operator!=( const BDay & , const BDay &); 
  6.       friend ostream & operator<<( ostream &, const BDay &);
  7.  
  8.                   public:
  9. ...
  10. ...
  11. ...
  12.                   private:
  13. ...
  14. ...
  15. ...
  16. }
  17.  
  18. Member Defintion:
  19.  
  20. bool operator!=( const BDay & lhs, const BDay & rhs )
  21. {
  22.      if (lhs.getName() != rhs.getName()|| lhs.getDay() != rhs.getDay()|| 
  23.          lhs.getMonth()!= rhs.getMonth())
  24.          return true;
  25.      else 
  26.          return false;
  27. }
  28.  
  29. bool operator==( const BDay & lhs, const BDay & rhs )
  30. {
  31.      return !(lhs != rhs);
  32. }
Errors Generated:

Bloodshed Dev C++:
28 C:\Dev-Cpp\CS311\bday.h `bool BDay::operator==(const BDay&, const BDay&)' must take exactly one argument
29 C:\Dev-Cpp\CS311\bday.h `bool BDay::operator!=(const BDay&, const BDay&)' must take exactly one argument

In Visual Studio C++ 2008 Express Edition:
1>c:\dev-cpp\cs311\bday.h(29) : error C2804: binary 'operator ==' has too many parameters
1>c:\dev-cpp\cs311\bday.h(30) : error C2804: binary 'operator !=' has too many parameters


It's obvious in the Dev error that the compiler is assuming that the overloaded '!=' and '==' are member functions with one too many parameters though they are declared globally.

Thanks for any help
Feb 1 '08 #1
2 1575
Ganon11
3,652 Expert 2GB
The functions are not declared globally - as you have them right now, they are declared as member functions. When the logical operators are included as part of the class definition, they accept one argument, which is compared to the *this object.

You can declare the functions outside the class body, in which case your current definitions are correct. They must be declared as friend functions inside the class declaration.
Feb 1 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
They must be declared as friend functions inside the class declaration.
Only if they need access to class private members. If they can operate by calling other class methods to get the data (preferred), they do not have to be friends.
Feb 2 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
14
by: ambar.shome | last post by:
Hi, As you know there are few operators in C++ which cant be overloaded. They are: .., .*, ::, ?: , new , delete , sizeof , typeid , static_casr , dynamic_cast , const_cast ,...
1
by: Dave Corby | last post by:
Hi all, I have an overloaded template function, and in one particular spot can't get the right version of it to be called. Everywhere else in the program the correct version is called. Here's...
44
by: bahadir.balban | last post by:
Hi, What's the best way to implement an overloaded function in C? For instance if you want to have generic print function for various structures, my implementation would be with a case...
13
by: olanglois | last post by:
Hi, I am trying to derive a new class that will add new functions but no new data members and the base class has overloaded operators (+,-,+=,-=,etc...) returning either (Base &) or (const Base)...
9
by: rohits123 | last post by:
I have an overload delete operator as below ////////////////////////////////// void operator delete(void* mem,int head_type) { mmHead local_Head = CPRMemory::GetMemoryHead(head_type);...
13
by: Tristan Wibberley | last post by:
Hi I've got implementing overloaded operator new and delete pretty much down. Just got to meet the alignment requirements of the class on which the operator is overloaded. But how does one...
3
by: simbasaurus | last post by:
Hello! I am working on a really big project, in which the global new and delete operators can be overloaded, by including the file "overloadedops.h". The graphical interface was written using...
3
by: jerry.teshirogi | last post by:
I have the following class and main: ////////////////////////////////////////////////////////// #include <iostream.h> class myVector { public: double x, y, z:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.