tags:

views:

82

answers:

0

I get the following errors for this code:

Error 1 : In function `_Function std::for_each(_InputIterator, _InputIterator, _Function) [with _InputIterator = std::_List_iterator, _Function = void (*)(int)]':

Error 2 line 16 with the bool.

int main(int argc,char *argv[])
{
    if (argc != 4)
    {
        cout << "Error input!";
        exit(1);
    }

    ifstream file (argv[3]);
    if (!file.is_open())
    {
        cout << "Not fount page file!";
        exit(2);
    }

    int page_max = atoi(argv[1]);
    if (page_max < 3)
        page_max = 3;
    cout << "There are " << page_max << " pages AND ";

    Mem_page *mp;

    if (!strcmp(argv[2], "fifo"))
    {
        cout <<"Using FIFO Algorithm" << endl;
        mp = new Fifo_page(page_max);
    }
    else if (!strcmp(argv[2], "lru"))
    {
        cout <<"Using LRU Algorithm" << endl;
        mp = new Lru_page(page_max);
    }
    else
    {
        cout <<"Using SCR Algorithm" << endl;
        mp = new Scr_page(page_max);
    }

    char page_id[10];

    while (!file.eof())
    {
        file.getline(page_id, sizeof(page_id));
        mp->do_page( atoi(page_id) );
        mp->show_page();
    }
    file.close();
    //mp->show_page();
    //delete mp;
    return 0;
}

void Fifo_page::do_page(int pid)
{
    total_count++;
    find_page(pid);
    if (page_ptr == mem_page.end())
    {
        fault_count++;
        rm_page();
        add_page(pid);
    }
}

void Lru_page::do_page(int pid)
{
    total_count++;
    find_page(pid);
    if (page_ptr == mem_page.end())
    {
        fault_count++;
        rm_page();
        add_page(pid);
    }
    else
    {
        mem_page.erase(page_ptr);
        add_page(pid);
    }
}

void print_page(int pid)
{
    cout << pid << "\t";
}

void printpage(Mempage pid)
{
    cout << pid.mem_page << "\t";
}

void Scr_page::show_page()
{
    cout << "There are the pages in the memory:" << endl;
    for_each(mem_page.begin(), mem_page.end(), print_page);
    cout << endl <<"The page fault rate is " << 100*(float
            fault_count/total_count      << "%" << endl;
        }

        void Scr_page::reset_bit()
{
    for (pageptr = mem_page.begin(); pageptr != mem_page.end(); pageptr++)
        (*pageptr).ref_bit = 0;
}

void Scr_page::add_page(int pid)
{
    Mempage temp = {pid, 0};
    mem_page.push_back(temp);
}

void Scr_page::rm_page()
{
    if ((int)mem_page.size() < max_page)
        return;
    for (pageptr = mem_page.begin(); pageptr != mem_page.end(); pageptr++)
    {
        if ( (*pageptr).ref_bit == 0)
        {
            mem_page.erase(pageptr);
            reset_bit();
            return;
        }
    }
    mem_page.pop_front();
    reset_bit();
    return;
}

void Scr_page::find_page(int pid)
{
    pageptr = find_if(mem_page.begin(), mem_page.end(), Check_page(pid));
}

void Mem_page::show_page()
{
    cout << "There are the pages in the memory:" << endl;
    for_each(mem_page.begin(), mem_page.end(), print_page);
    cout << endl <<"The page fault rate is " << 100*(float)fault_count/total_count
         << "%" << endl;
}

void Mem_page::rm_page()
{
    if ((int)mem_page.size() == max_page)
    {
        mem_page.pop_front();
    }
}

void Mem_page::find_page(int pid)
{
    page_ptr = find(mem_page.begin(), mem_page.end(), pid);
}

void Mem_page::add_page(int pid)
{
    mem_page.push_back(pid);
}


void Scr_page::do_page(int pid)
{
    total_count++;
    find_page(pid);
    if (pageptr == mem_page.end())
    {
        fault_count++;
        rm_page();
        add_page(pid);
    }
    else
    {
        (*pageptr).ref_bit = 1;
    }
}

void print_page(Mempage pid)
{
    cout << pid.mem_page << "\t";
}