Equivalency Semantics, Operator Overloading

G

Grant Austin

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::eek:perator==(Page&, Page&)' must take exactly one argument
/usr/include/c++/3.2.2/bits/stl_algo.h: In function `_InputIter
std::find(_InputIter, _InputIter, const _Tp&, std::input_iterator_tag) [with
_InputIter = std::_List_iterator<Page, Page&, Page*>, _Tp = Page]':
/usr/include/c++/3.2.2/bits/stl_algo.h:298: instantiated from `_InputIter std:
:find(_InputIter, _InputIter, const _Tp&) [with _InputIter = std::_List_iterator
<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::eek:perator==(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<<(ostream& 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...please wait" << endl;
}
list<Page>::iterator 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>::iterator found = find(memory.begin(),memory.end(),*p);
cout << *p << " taht's it " << endl;

if(memory.size() < frames){
memory.push_back(*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_back(*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.print(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(ostream& 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_front();
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>::iterator iter;
};

#endif
 
P

Peter van Merkerk

Grant Austin said:
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...
class Page{
public:
bool operator== (Page & lhs, Page & rhs){
return (lhs.page == rhs.page);
}

Change this function to:

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

Grant Austin

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
 
P

Peter van Merkerk

Grant Austin said:
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...changes 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top