Ошибки реализации планировщика в C ++ - PullRequest
0 голосов
/ 03 апреля 2020

При компиляции я получаю следующие сообщения:

планировщик (копия). cpp: 18: 46: ошибка: «CompareTaskTime» не было объявлено в этой области

планировщик (копия). cpp: 18: 61: ошибка: недопустимый аргумент шаблона 3

планировщик (копия). cpp: 19: 11: ошибка: запрос элемента 'push' in 'prque ', который имеет неклассовый тип' int '

планировщик (копия). cpp: 20: 11: ошибка: запрос на член' push 'в' prque ', который не является классом тип 'int'

планировщик (копия). cpp: 21: 11: ошибка: запрос на член 'push' в 'prque', который имеет неклассовый тип 'int'

планировщик (копия). cpp: 23: 18: ошибка: запрос на член 'top' в 'prque', который имеет неклассовый тип 'int'

Может кто-нибудь объяснить мне в чем причина и что мне нужно изменить в моем коде?

код ...

планировщик. cpp:

#include "scheduler.hpp"
#include <queue>
#include <vector> //database, an array of the elements. no need to do mallooc. 
int main ()
{   
    //Scheduler1 sch;
    //objects
   Task1 task1(5);
   Task2 task2 (15,20,30);
   Task3 task3 (10,40,30);

   /*
   task1.execute();
   task2.execute();
   task3.execute();
  */

    priority_queue<TaskInfo,vector<TaskInfo>,CompareTaskTime> prque; //prque is an object of a priority_queue and its type is TASKINFO  
    prque.push(task1); 
    prque.push(task2); 
    prque.push(task3); 

    cout <<prque.top().tickTrigger>> endl;




    return 0; 
} 

планировщик. hpp:

    #ifndef __SCHEDLUER_HPP__
#define __SCHEDLUER_HPP__ 

//declaration. includes,classes,functions, global variables,
#include <iostream>
using namespace std;
class TaskInfo
{

    public: //in order to prevent from other class to change their values directly except the inherited class.
    //declaration
        uint taskID;
        uint taskNum;
        uint tickTrigger;


    TaskInfo(uint trigerWithin)
    {
        tickTrigger=trigerWithin;
    }

    int execute ();
    ~TaskInfo(){}


};

class Task1: public TaskInfo 
{ 
    public:
    //constructor.
    Task1(uint trigerWithin):TaskInfo(trigerWithin)
    {

    }

    ~Task1(){}

    //method
  int execute()
  {
    bool result=login();
    if(result==true)
    {
        return 0;
    }
    return -1;

  }

  private:

  bool login()
  {
      if (tickTrigger<10)
      {
        return true;   
      }

       return false;
   }



};

class Task2: public TaskInfo 
{ 
    public:
    int y,z;
    //constructor.
    //a is for triggerwithin
    Task2(int a,int b, int c):TaskInfo(a)
    {
        y=b,z=c;
    }

    ~Task2(){}

    //method
  int execute()
  {
    bool result=calc();
    if(result==true)
    {
        return 0;
    }
    return -1;

  }

  private:

  bool calc()
  {
      return y+z;
   }


};

class Task3:public TaskInfo
{
    public:
    int x,y;
    //constructor.
    //c is for triggerwithin
    Task3(int a, int b, int c):TaskInfo(c)
    {
       x=a,y=b;
    }

    ~Task3(){}

    //method
  void execute()
  {
        draw();

  }

  private:

  void draw()
  {
      printf ("%d %d",x,y);
      //return true;

  }


};

/*class Scheduler1
{

   public:
   Scheduler1()
   {
      //empty constructor  
   }
      const uint TICK_UNIT = 1000;
      uint ticksSoFar;
      queue<TaskInfo*> taskQueue;

    void showpq(queue <TaskInfo> gq) 
    { 
       queue <TaskInfo> g = gq; //generic
    //while (!g.empty()) 
    //{ 
    //    cout << '\t' << g.top(); 
    //    g.pop(); 
    ////} 
    //cout << '\n'; 
    } 

    void regist (TaskInfo* pTaskInf,unsigned triggerWithin)
    {
        //set the tickTrigger of the pending task

        pTaskInf->tickTrigger= ticksSoFar + triggerWithin;

        //push to the right position in queue
        this->taskQueue.push(pTaskInf);

    }

      ~Scheduler1(){} //empty destrctor 


    private:
     void timer(){
         while (true)
         {
             ticksSoFar++;
             //top=peek.
             while (!taskQueue.empty() && taskQueue.top()->tickTrigger<= ticksSoFar)
             {
                 TaskInfo* pNextTask= taskQueue.top();

                 //invoke next task
                 pNextTask->execute();

                 //task done. remove it from queue           
                 taskQueue.pop();
                 delete pNextTask;
              } 
        }       

    }



};

*/
/*class queue
{
   struct node {
    TaskInfo data;
    struct node* next;
    };

struct node* front= NULL;
struct node* rear= NULL;
struct node* temp;

public:

void Insert (TaskInfo data) {

    if (rear == NULL) {
      rear = (struct node *)malloc(sizeof(struct node));
      rear->next = NULL;
      rear->data = data; //members
      front = rear;
   } else {
      temp=(struct node *)malloc(sizeof(struct node));
      rear->next = temp;
      temp->data = data;
      temp->next = NULL;
      rear = temp;
   }
}

void Display() {
   temp = front;
   if ((front == NULL) && (rear == NULL)) {
      cout<<"Queue is empty"<<endl;
      return;
   }
   cout<<"Queue elements are: ";
   while (temp != NULL) {
      cout<<temp->data.taskID<<" ";
      temp = temp->next;
   }
   cout<<endl;
}

TaskInfo top(){ //returns the first element. 
    temp=front;
    return temp->data;

}

void pop() { //deletes the last element in the list (th.
   temp = front;
   if (front == NULL) {
      cout<<"enpty"<<endl;
      return;
   }
   else {
   if (temp->next == NULL) { //one ekement.last element in the linked list,no more elements later on
       free(front); //remove it
       front = NULL;
       rear = NULL;
     } else { //number of elements
      temp = temp->next;
      cout<<"Element deleted from queue is : "<<(front->data.taskID)<<endl;
      free(front); //remove
      front = temp; //mext elements.
      cout<<"Element deleted from queue is : "<<(front->data.taskID)<<endl;
     }
    }       
    }
int size() {

int count=0;

struct node* front=NULL;

while (front!=NULL) 
{
    count++;
}

return count;

}
};
*/


class CompareTaskTime 
{
    public:
       //overwrite.
       bool operator() (TaskInfo& Task1, TaskInfo& Task2)
       {
        return Task1.tickTrigger<Task2.tickTrigger; 
       }


};

#endif
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...