【问题标题】:how can i get the front or top element of a vector queue?如何获取向量队列的前元素或顶部元素?
【发布时间】:2016-12-06 06:27:13
【问题描述】:

我没有放完整的代码,因为它很长,我只需要小部分的帮助,即 **** 区域。我似乎无法使用 front() 或 top() 来获取队列的顶部元素。我尝试让 top() 函数 List 不断出错:1) 类 List 没有名为“top”的成员,这意味着我在 List 中没有函数 top,当我让它说 2) 不匹配 'operator= ' in printer_cpu[i] = SList::top() with T=PCB]()'

template <class T>
class node{
public:
T data;
node *next;
};

template <class T>
class List{

node<T> *head;
node<T> *tail;

public:

List()
{
    head = tail = NULL;
}

bool isEmpty()
{
    if(head == NULL) return true;
    else             return false;
}

void enqueue(T new_data){
    node<T> *temp = new node<T>;
    temp->data = new_data;
    temp->next = NULL;
    if(isEmpty()){
        head = temp;
        tail = temp;
    }
    else{
        tail->next = temp;
        tail = temp;
    }
}
void dequeue(){
    if(isEmpty())
    {
        cout << "The list is already empty" << endl;
    }

    node<T>* temp;
    if(head == tail){
        temp->data=head->data;
        delete head;
        head = tail = NULL;
    }
    else{
        temp->data = head->data;
        head = head->next;
        delete temp;
    }
}
node<T> top()  // need help here ****
{ 
     return head;
}

void display(){
    node<T> *current = head;
    while(current != NULL){
        cout << current->data << endl;
        current = current->next;
    }
}


};


struct PCB
{
    int ProcessID;
    int ProcessorSize;
    int priority;
    string name;
};
typedef List<PCB> printing;
typedef List<PCB> disk;

void gen(vector<printing> &printer_queue,string printer_name[], int printers)
{
    for(int i = 0; i < printers; i++)
    {
        int num = i+1;
        ostringstream convert;
        convert << num;
        printer_name[i] = "p" + convert.str();
        printer_queue.push_back(printing());
    }
int main()
{
        int numOfPrinter = 5;
        string interrupt;
        cin >> interrupt;
        PCB cpu;
        PCB printer_cpu[numOfPrinter];
        string printer_name[numOfPrinter];
        vector<printing> PQ;
        gen(PQ,printer_name,numOfPrinter);
        for(int i = 0; i < numOfPrinter; i++)
        {
              if(interrupt == printer_name[i])
              {
                   cout << "Enter a name for this printer file: " << endl;
                   cin >> cpu.name;
                   PQ[i].enqueue(cpu);
                   printer_cpu[i] = PQ[i].top(); //need help here ****
              }

        }
}

【问题讨论】:

    标签: c++ vector linked-list queue


    【解决方案1】:

    看起来你缺少一个星号,因为你需要返回类型指针,因为这就是 head 。 你应该有

    node<T> * top() 
    {
      ...
    }
    

    您还需要重载 = 运算符,因为您正在尝试将类型 PCB 与类型节点 * 进行比较。

    【讨论】:

    • 不模板类整个事情使它类型T?它给了我一个 T 类的错误声明
    • 你是对的——我没有看到你在一堂课上就掌握了所有这些
    • 也许您忘记了星号?节点 * top()?
    • 哦,试试 node::Node * top()
    • nope :/ 这里的错误是我在 printer_cpu[i] 中无法匹配 'operator=' = 一长串东西
    【解决方案2】:

    嗯,在纠正了一些错误之后,我成功编译了你的代码。

    我没有遇到class List has no memeber named 'top'的问题。

    那么你的top()函数返回head的值,所以你应该把它改成:node&lt;T&gt;* top(),因为head是一个指向node&lt;T&gt;的指针。

    你得到no match for 'operator='错误的原因是printer_cpu[i]的类型是PCB,而PQ[i].top()的类型应该是node&lt;T&gt;*

    我还发现您发布的代码在int main() 之前缺少}

    【讨论】:

    • hmmm 也许我的类型的问题必须在 PCB 中:/
    • 有没有办法修复'operator='不匹配的问题?
    • 嘿,你认为你可以帮助我操作 operator= 吗?我试着写了好几次,它不会让它工作。它不会接受另一个变量或向量
    • bool operator = (const PCB &c) { ProcessID = c.ProcessID; }
    • printer_cpu[i] = PQ[i].top(); printer_cpu[i] 的类型是 PCBPQ[i].top() 的类型是 node&lt;PCB&gt;*,所以你得到了 operation= errror。你可以改成printer_cpu[i] = PQ[i].top()-&gt;data
    猜你喜欢
    • 1970-01-01
    • 2015-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    相关资源
    最近更新 更多