473,386 Members | 1,606 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,386 software developers and data experts.

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(_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::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<<(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
Jul 22 '05 #1
3 1749

"Grant Austin" <ga*****@foo.foo.bar.net> wrote in message
news:pa****************************@foo.foo.bar.ne t...
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.merkerk(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.foo.bar.net> wrote in message
news:pa****************************@foo.foo.bar.ne t...
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.

--
Peter van Merkerk
peter.van.merkerk(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
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,...
34
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...
16
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...
2
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...
1
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...
67
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...
3
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
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,...
12
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...

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.