【问题标题】:Compiler error with priority queue implementation优先级队列实现的编译器错误
【发布时间】:2018-03-22 17:35:36
【问题描述】:

我正在处理一项使用#include 库创建优先级队列的任务。我写了几个函数,遇到了一个我想不通的问题。

// CSCI 2530
// Assignment: 6
// Author:     
// File:       pqueue.cpp
// Tab stops:  ***

// **Say what this program does here.  (replace this comment)**


#include <cstdio>
#include "stdafx.h"
#include "pqueue.h"

using namespace std;



//Structure PQCell holds an item (item), priority (priority) and
//a pointer to the next item in the priority queue.

struct PQCell 
{
    ItemType item;
    PriorityType priority;
    PQCell* node;

    PQCell() : item(0), priority(0), node()
    {
    }
};

//Checks the the first element of the linked list (q) for
//a NULL value. If the list is empty this first element will be NULL.

bool isEmpty(const PriorityQueue& q)
{
    if (q.next == NULL)
    {
        return false;
    }
    return true;
}
//-------------------------------------------------------------------


void insertCell(PQCell*& L, ItemType x, PriorityType p)
{
    PQCell cell;
    cell.item = x;
    cell.priority = p;
}



void insert(PriorityQueue& q, ItemType x, PriorityType p)
{
    insertCell(q, x, p);
}


int main()
{



    return 0;
}

在上面的代码中,它显示了我编写了“insert”和“insertCell”函数的程序的主文件。

insertCell 函数应该在调用时将一个新单元插入 PriorityQueue。但是,当我尝试调用插入单元格时,它给了我错误(如上图所示)。

另外,这是我为这个项目创建的头文件

// CSCI 2530
// Assignment: ***
// Author:     ***
// File:       ***
// Tab stops:  ***

// **Say what this program does here.  (replace this comment)**


#include <cstdio>
using namespace std;

struct PQCell;
//Type definitions

typedef const char* ItemType;
typedef double      PriorityType;

struct PriorityQueue
{

    PriorityQueue* next;

    PriorityQueue()
    {
        next = NULL;
    }
};
//Prototypes

bool isEmpty(const PriorityQueue& q);
void insert(PriorityQueue& q, ItemType x, PriorityType p);

另外,这里是分配说明的链接..... http://cs.ecu.edu/~karl/2530/spr18/Assn/Assn6/assn6.html

【问题讨论】:

  • 发布实际代码而不是截图链接
  • PriorityQueue 和 PQCell 是完全不相关的类型,但您正试图将 PriorityQueue 传递给一个函数,该函数接受指向 PQCell 的指针。你认为这会如何运作?
  • 我试图复制您的屏幕快照并粘贴到我的 IDE 中,但我的 IDE 不理解屏幕快照。没有代码作为文本 == 没有帮助。

标签: c++ linked-list priority-queue


【解决方案1】:

您正在尝试将 PriorityQueue 的引用作为第一个参数传递给 insertCell,此函数需要一个 PQCell 作为参数。

我会说你的 insertCell 应该请求 PriorityQueue,这样你就可以推入它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-09-27
    • 1970-01-01
    • 1970-01-01
    • 2012-11-26
    • 1970-01-01
    • 1970-01-01
    • 2015-01-06
    • 1970-01-01
    相关资源
    最近更新 更多