473,657 Members | 2,845 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Equivalency Semantics, Operator Overloading

Hi all,

I'm trying to overload the '==' operator for a class, Page, that I wrote
so that I can use the STL List 'find' algorithm. I'm having difficulties
getting a definition that g++ likes and that satisfies the requirements
for STL. The only criteria I care about for equivalency are that the
Page.page properties are equal.

Below I'm including all of the errors and the two pertinent files. I
apologize for including so much...I'm just having a really hard time with
this and if anyone can help I'd like to provide all the info I have...

Errors:

Page.h:26: `bool Page::operator= =(Page&, Page&)' must take exactly one argument
/usr/include/c++/3.2.2/bits/stl_algo.h: In function `_InputIter
std::find(_Inpu tIter, _InputIter, const _Tp&, std::input_iter ator_tag) [with
_InputIter = std::_List_iter ator<Page, Page&, Page*>, _Tp = Page]':
/usr/include/c++/3.2.2/bits/stl_algo.h:298: instantiated from `_InputIter std:
:find(_InputIte r, _InputIter, const _Tp&) [with _InputIter = std::_List_iter ator
<Page, Page&, Page*>, _Tp = Page]'
FIFO_OS.h:24: instantiated from here
/usr/include/c++/3.2.2/bits/stl_algo.h:172: no match for `Page& == const Page&'
operator
Page.h:26: candidates are: bool Page::operator= =(Page&, Page&)

/*
Page class header
*/
#ifndef PAGE
#define PAGE

#include "general.h"

class Page{
public:
Page(long int addr,int act){
address = addr;
page = (address & 0xFFFFF000);
offset = (address & 0x00000FFF);
dirty = false;
}

void print(ostream& out){
out << "address: " << address << endl;
out << "page: " << page << endl;
out << "offset: " << offset << endl;
out << "dirty: " << SHOWBOOL(dirty) << endl;
}

long int getpage(){
return page;
}

bool operator== (Page & lhs, Page & rhs){
return (lhs.page == rhs.page);
}

private:
long int address, page, offset;
bool dirty;
};

ostream & operator<<(ostr eam& out, Page p){
p.print(out);
return out;
}

#endif

/*
FIFO algorithm header
*/
#ifndef FIFO
#define FIFO

#include <list>
#include "Page.h"
#include "clsStat.h"
#include "general.h"

class FIFO_ALGORITHM {
public:
FIFO_ALGORITHM( list<Page> & input){
data = input;
frames = FRAME_LIMIT;
}

void apply(){
if(OSUPPRESS){
cout << "applying FIFO algorithm...ple ase wait" << endl;
}
list<Page>::ite rator p;
p = data.begin();
while(p != data.end()){
/* if *p is in memory do nothing but increment a hit else increment a miss **** needs to be added!!! */
list<Page>::ite rator found = find(memory.beg in(),memory.end (),*p);
cout << *p << " taht's it " << endl;

if(memory.size( ) < frames){
memory.push_bac k(*p);
statistics.num_ refs = statistics.num_ refs + 1;
if(!OSUPPRESS){
print(cout,*p);
}
}else {
statistics.num_ faults = statistics.num_ faults + 1;
if(evict()){
statistics.num_ refs = statistics.num_ refs + 1;
memory.push_bac k(*p);
if(!OSUPPRESS){
print(cout,*p);
}
} // if we successfully evicted a page then we can queue up our page

}
/* get next reference */
data.pop_front( );
p = data.begin();
}
cout << " -- FIFO Algorithm Statistics -- " << endl;
statistics.prin t(cout);
}

void print(ostream& out, Page p){
out << p.getpage() << ": ";
iter = memory.begin();
while(iter != memory.end())
{
out << (*iter).getpage () << " ";
iter++;
}
out << endl;
}

void output_stats(os tream& out){
/* use this for << operator */
}

bool evict(){
/* simple for FIFO but using same methods so when I copy and paste for my LRU algorithm I know what
the hell is going on.(maybe)
*/
memory.pop_fron t();
if(memory.size( ) < frames){
statistics.num_ evictions = statistics.num_ evictions + 1;
return true;
}
else
return false;
}

private:
int frames;
clsStat statistics;
list<Page> data;
list<Page> memory;
list<Page>::ite rator iter;
};

#endif
Jul 22 '05 #1
3 1760

"Grant Austin" <ga*****@foo.fo o.bar.net> wrote in message
news:pa******** *************** *****@foo.foo.b ar.net...
Hi all,

I'm trying to overload the '==' operator for a class, Page, that I wrote
so that I can use the STL List 'find' algorithm. I'm having difficulties
getting a definition that g++ likes and that satisfies the requirements
for STL. The only criteria I care about for equivalency are that the
Page.page properties are equal.

Below I'm including all of the errors and the two pertinent files. I
apologize for including so much...I'm just having a really hard time with
this and if anyone can help I'd like to provide all the info I have... <snip> class Page{
public: <snip> bool operator== (Page & lhs, Page & rhs){
return (lhs.page == rhs.page);
}


Change this function to:

bool operator== (Page & rhs) const
{
return (page == rhs.page);
}

--
Peter van Merkerk
peter.van.merke rk(at)dse.nl
Jul 22 '05 #2
Thanks it's working now. I had to do something slightly different
than what you said:

bool operator== (Page & rhs) const
{
return (page == rhs.page);
}

and use:
bool operator== (const Page & rhs) const
{
return (page == rhs.page);
}

I'm not sure what the second const is for...

Thanks again,
Grant
Jul 22 '05 #3

"Grant Austin" <ga*****@foo.fo o.bar.net> wrote in message
news:pa******** *************** *****@foo.foo.b ar.net...
Thanks it's working now. I had to do something slightly different
than what you said:

bool operator== (Page & rhs) const
{
return (page == rhs.page);
}

and use:
bool operator== (const Page & rhs) const
{
return (page == rhs.page);
}

I'm not sure what the second const is for...


I overlooked that one, obviously you don't want operator==() to change rhs.
The second const tells the compiler that that memeber function is not
supposed to change the object.

For example suppose you make a little mistake like this:

bool operator== (const Page & rhs) const
{
return (page = rhs.page); // Whoops...change s page member!!!
}

Because the operator==() function is declared const the compiler will
refuse to compile this code. Without the last const the compiler would
compile the code as if there is nothing wrong with it, and you will spend
hours of debugging trying to figure out why the page member sometimes just
changes.

The rule of the thumb is to use const whenever possible. With a little
amount of extra typing can save yourself hours of debugging misery.

--
Peter van Merkerk
peter.van.merke rk(at)dse.nl




Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
2597
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class, but I would like to use internally my own = operator for some of my value types, so I can say "x = y;" rather than "x.Assign(y);". The op_Assign operator seems impossibly broken since it takes __value copies of the two objects. Perhaps there is...
34
6438
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment operator. Here's what I'm trying to do. I've defined class Complex as class Complex { friend ostream &operator<<( ostream &, Complex & ); public: Complex( float = 0.0, float = 0.0 );
16
3081
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class has two int memebers "dataA", "dataB". The derived class has an additional int member "dataC". I am simply trying to overload the + operator so that 'adding' two objects will sum up the corresponding int members.
2
2057
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the overloading of operator<<. In the .cc of the respective class or in a file where I am overloading operator<< for all classes? Cheers,
1
1539
by: bluekite2000 | last post by:
In other words Array<int,1> A(5), B(10); A = B(Range(0,4)); // Statement 1 Array<int,1> C = B(Range(0,4)); // Statement 2 I d like Statement 1 results in a portion of B's data being copied into A and After Statement 2 is executed, the array C is a reference (or alias) to
67
8631
by: carlos | last post by:
Curious: Why wasnt a primitive exponentiation operator not added to C99? And, are there requests to do so in the next std revision? Justification for doing so: C and C++ are increasingly used in low-level numerical computations, replacing Fortran in newer projects. Check, for example, sourceforge.net or freshmeat.net But neither language offers a primitive exp operator.
3
2288
by: karthik | last post by:
The * operator behaves in 2 different ways. It is used as the value at address operator as well as the multiplication operator. Does this mean * is overloaded in c?
3
3272
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj, obj2 ;
12
2446
by: terminator | last post by:
the following compiles unless the first line is uncommented . when I try to uncomment the first line I get: error : 'B &FM::operator ,(FM::mystruct<A>,B &)' : could not deduce template argument for 'FM::mystruct<A>' from 'std::basic_string<_Elem,_Traits,_Ax>::_Myt' with As long as 'mystruct' is a normal struct no error occures,but there is
0
8384
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8302
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8820
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8718
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8499
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8601
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5630
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.