mercredi 18 avril 2018

access friend function in constructor

I'm trying to run returnGoodPID() within liveProcess's constructor. readyQ contains a vector of liveProcess objects. What I'd like to do is use returnGoodPID() to return the lowest unused PID to the liveProcess constructor. Compiler notifies me that this usage requires returnGoodPID to be static, but declaring static, the compiler then tells me that usedPIDs is being accessed in a static function. I'm not sure how to resolve this issue.

class liveProcess : process {

   liveProcess() {
       this->pid = readyQueue::returnGoodPID();
   }
   friend int readyQueue::returnGoodPID();
   private:
       int pid;
};

class readyQueue {
public:
   std::set<int> usedPIDs;
   //find lowest absent int in set.
   //adds int to set and returns the int for use in liveProcess constructor
   int returnGoodPID(){
      std::set<int>::iterator it;
      int testPID = 0;
      int foundPID = 0;

      for(testPID; testPID < 1000; testPID++) {
            it = readyQueue::usedPIDs.find(testPID);
            if(it == usedPIDs.end()) {
                 usedPIDs.insert(testPID);
                 foundPID = testPID;
                 testPID = 1001;
             }
       }
       return foundPID;
   }
private:
std::vector<liveProcess> readyq;


};

Aucun commentaire:

Enregistrer un commentaire