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:
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:
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
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:
/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:
/*
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