-2

Main.cpp

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <queue>
#include<functional>
#include "procedure.h"

using namespace std;


int gcounter = 0;

//Comparator Classes
struct ComparebyJobTime{
        bool operator()(Procedure lhs,Procedure rhs) {
            return lhs.getTime() < rhs.getTime();
        }
};

struct ComparebyProc{
        bool operator()(Procedure lhs, Procedure rhs){
            return lhs.getProc() < rhs.getProc();
        }
};


typedef priority_queue<Procedure, vector<Procedure>, ComparebyProc> fcfs_q;
typedef priority_queue<Procedure, vector<Procedure>, ComparebyJobTime> sjf_q;

//Scheduling Algorithms
void FCFS(fcfs_q procs, int n, int m, int t_cs){
    cout << "time " << gcounter << "ms: Simulator started for FCFS [Q ";
    for(fcfs_q::iterator it = procs.begin(); it!= procs.end(), ++it{
        if(it == (procs.end() - 1){
            cout << *it.getProc() << "]" << endl;
        }
        else{
            cout << *it.getProc() << " ";
        }
    }
}

void SJF(sfj_q procs, int n, int m, int t_cs){
    cout << "time " << gcounter << "ms: Simulator started for SJF [Q ";
    for(sjf_q::iterator it = procs.begin(); it != procs.end(); ++i{
        if(it == (procs.end() - 1){
            cout << *it.getProc() << "]" << endl;
        }
        else{
            cout << *it.getProc() << " ";
        }
    }
}

//Main function
int main(int argc, char * argv[]) {
    if (argc != 3){
        cerr << "Usage: " << argv[0] << "processes-file\n";
    }
    ifstream in_file(argv[1]);

    if(!in_file.good()){
        cerr << "Can not open the input file " << argv[1] << "\n";
    }
    ofstream out_str(argv[2]);
    if(!out_str){
        cerr << "Could not open " << argv[2] << "to write\n";
    }

    string line;
    fcfs_q pqf;
    sjf_q pqs;

    while(getline(in_file, line)){
        istringstream iss(line);
        char com;
        iss >> com;
        if(line.length() == 0 || com == '#'){
            continue;
        }
        vector<string> split;
        string token;
        stringstream temp(line);
        while(getline(temp, token, '|')){
            split.push_back(token);
        }
        pqf.push(Procedure(atoi(split[0].c_str()),atoi(split[1].c_str()),atoi(split[2].c_str()),atoi(split[3].c_str())));
        pqs.push(Procedure(atoi(split[0].c_str()),atoi(split[1].c_str()),atoi(split[2].c_str()),atoi(split[3].c_str())));
    }
    //Simulation Start
    int n = pqf.size();
    int m = 1; //Number of processors
    int t_cs = 9; //Default context switch time
    FCFS(pqf, n, m, t_cs);
    cout << "time " << gcounter << "ms: Simulator ended for FCFS" << endl;
    SJF(pqs, n, m, t_cs);
    cout << "time " << gcounter << "ms: Simulator ended for SJF" << endl;
}

procedures.h

class Procedure{
    public:
        Procedure();
        Procedure(int p, int bt, int num, int io){
            procNum = p;
            burst = bt;
            numBurst = num;
            ioTime = io; 
        }
        int getProc(){return procNum;}
        int getNum(){return numBurst;}
        int getIO(){return ioTime;}
        int getTime(){return burst;}

    private:
        int procNum;
        int numBurst;
        int ioTime;
        int burst;
};

Errors:

hello-cpp-world.cc: In function ‘void FCFS(fcfs_q, int, int, int)’:
hello-cpp-world.cc:39:9: error: ‘iterator’ is not a member of ‘fcfs_q {aka std::priority_queue<Procedure, std::vector<Procedure>, ComparebyProc>}’
     for(fcfs_q::iterator it = procs.begin(); it!= procs.end(), ++it{
         ^
hello-cpp-world.cc:39:26: error: expected ‘;’ before ‘it’
     for(fcfs_q::iterator it = procs.begin(); it!= procs.end(), ++it{
                          ^
hello-cpp-world.cc:39:46: error: ‘it’ was not declared in this scope
     for(fcfs_q::iterator it = procs.begin(); it!= procs.end(), ++it{
                                              ^
hello-cpp-world.cc:39:57: error: ‘fcfs_q’ has no member named ‘end’
     for(fcfs_q::iterator it = procs.begin(); it!= procs.end(), ++it{
                                                         ^
hello-cpp-world.cc:39:68: error: expected ‘;’ before ‘{’ token
     for(fcfs_q::iterator it = procs.begin(); it!= procs.end(), ++it{
                                                                    ^
hello-cpp-world.cc:39:68: error: expected primary-expression before ‘{’ token
hello-cpp-world.cc:39:68: error: expected ‘)’ before ‘{’ token
hello-cpp-world.cc:40:25: error: ‘fcfs_q’ has no member named ‘end’
         if(it == (procs.end() - 1){
                         ^
hello-cpp-world.cc:40:35: error: expected ‘)’ before ‘{’ token
         if(it == (procs.end() - 1){
                                   ^
hello-cpp-world.cc:46:5: error: expected primary-expression before ‘}’ token
     }
     ^
hello-cpp-world.cc:46:5: error: expected ‘;’ before ‘}’ token
hello-cpp-world.cc: At global scope:
hello-cpp-world.cc:49:10: error: variable or field ‘SJF’ declared void
 void SJF(sfj_q procs, int n, int m, int t_cs){
          ^
hello-cpp-world.cc:49:10: error: ‘sfj_q’ was not declared in this scope
hello-cpp-world.cc:49:23: error: expected primary-expression before ‘int’
 void SJF(sfj_q procs, int n, int m, int t_cs){
                       ^
hello-cpp-world.cc:49:30: error: expected primary-expression before ‘int’
 void SJF(sfj_q procs, int n, int m, int t_cs){
                              ^
hello-cpp-world.cc:49:37: error: expected primary-expression before ‘int’
 void SJF(sfj_q procs, int n, int m, int t_cs){
                                     ^

With this above block of code and a defined procedure.h file, I am getting errors saying that my typedef iterator isn't a valid class. I can't figure out what is causing this can anyone see errors in my code above?

Nick
  • 53
  • 1
  • 8
  • 1
    When posting questions about build error, please include the actual, complete, in full and unedited error output in the question body, including any informational notes. – Some programmer dude Feb 15 '16 at 08:10
  • 1
    It's the same problem as a priority queue of, say, `int`. Nothing to do with your own type. Consult a [reference](http://en.cppreference.com/w/cpp/container/priority_queue) for the available operations. – chris Feb 15 '16 at 08:11
  • I have updated it @JoachimPileborg – Nick Feb 15 '16 at 08:14
  • Priority queue doesnt have an iterator http://stackoverflow.com/a/20304856/2000547 – Nit Feb 15 '16 at 08:21
  • See here: http://stackoverflow.com/questions/4484767/how-to-iterate-over-a-priority-queue and here: http://stackoverflow.com/questions/1259099/stl-queue-iteration In short: both `std:queue` and `std::priority_queue` aren't meant to be iterable. Change your approach to the problem or use a different container. – iksemyonov Feb 15 '16 at 08:22

1 Answers1

0

Neither std:queue nor std::priority_queue are iterable, hence the types and methods you're trying to call are not defined. You could emulate iteration over the queue like show below, but beware: this operation is going to destroy the queue. Since you are passing the queue object by value, rather than by reference, the original one isn't going to be destroyed, only the copy, but the copying itself may take some time.

void FCFS(fcfs_q procs, int n, int m, int t_cs){
    cout << "time " << gcounter << "ms: Simulator started for FCFS [Q ";
    while (!fcfs.empty()) {
        const auto& elem = fcfs.top();
        if(fcfs.size() == 1){ // the last element in the queue
            cout << elem.getProc() << "]" << endl;
        }
        else{
            cout << elem.getProc() << " ";
        }
        fcfs.pop();
    }
}

See How to iterate over a priority_queue? and STL queue iteration for further reading.

Also, maybe look into the STL heap algorithms, such as make_heap(), push_heap(), pop_heap() and sort_heap() in <algorithm>. Those allow you to explicitly store the items a container of your choice, retaining all its properties like, for example, the ability to iterate over it, while keeping it sorted.

Community
  • 1
  • 1
iksemyonov
  • 4,106
  • 1
  • 22
  • 42