指向结构指针的指针(优先队列)
Pointer to Pointer to Structure (Priority Queue)
我是初学者(在 C++ 方面,我来自 C(6 个月的经验)),我正在尝试创建一个优先级队列,但有些东西不起作用。
当我启动程序并编译它时,没有错误。但是屏幕上什么也没有打印,程序崩溃了。
代码如下:
PriorityQueue.h
using namespace std;
class PriorityQueue{
private:
struct pqentry_t{
string value;
float priority;
};
pqentry_t **_pqentry;
int _size;
int _next;
public:
PriorityQueue();
~PriorityQueue();
void insert(string value, float priority);
void printQueue();
};
PriorityQueue.cpp
#include <iostream>
#include <string>
#include "PriorityQueue.h"
#define SIZE 8
using namespace std;
PriorityQueue::PriorityQueue(){
_size = SIZE;
_next = 0;
_pqentry = new pqentry_t*[_size];
}
PriorityQueue::~PriorityQueue(){
delete[] _pqentry;
}
void PriorityQueue::insert(string value, float priority){
_pqentry[_next]->value = value; // this is probably not working
_pqentry[_next]->priority = priority;
_next++;
}
void PriorityQueue::printQueue(){
for (int i = 0; i < _next; i++){
cout << "Value: " << _pqentry[i]->value << endl
<< "Priority: " << _pqentry[i]->priority << endl;
}
cout << endl;
}
main.cpp
#include <iostream>
#include <string>
#include "PriorityQueue.h"
using namespace std;
int main()
{
PriorityQueue pq;
pq.insert("Banana", 23);
pq.printQueue();
}
我想,我知道错误在哪里,在PriorityQueue.cpp,这里:
_pqentry[_next]->value = value;
_pqentry[_next]->priority = priority;
但我不知道出了什么问题,也无法修复。编译器说没有错误。
我希望,你能帮助我。提前致谢!
您确实分配了 _pqentry 成员,但您还需要分配此数组的每个条目,例如:
_pqentry[_next] = 新 pqentry_t;
在写入之前。
并且不要忘记删除那些:)
看起来您正在构造函数中创建一个指向 pqentry_t 的指针数组,但您的插入方法期望它本身是一个 _pqentry 结构数组。您没有为 pqentry_t 元素本身分配 space,因此当您尝试在插入方法中取消引用它们时,程序会崩溃。
尝试将 class 中 _pqentry 的定义更改为 pqentry_t *_pqentry,并将构造函数中的分配更改为新的 pqentry_t[size]。这将允许您的 insert 和 printQueue 方法在写入时访问 _pqentry 的条目。
我是初学者(在 C++ 方面,我来自 C(6 个月的经验)),我正在尝试创建一个优先级队列,但有些东西不起作用。 当我启动程序并编译它时,没有错误。但是屏幕上什么也没有打印,程序崩溃了。
代码如下:
PriorityQueue.h
using namespace std;
class PriorityQueue{
private:
struct pqentry_t{
string value;
float priority;
};
pqentry_t **_pqentry;
int _size;
int _next;
public:
PriorityQueue();
~PriorityQueue();
void insert(string value, float priority);
void printQueue();
};
PriorityQueue.cpp
#include <iostream>
#include <string>
#include "PriorityQueue.h"
#define SIZE 8
using namespace std;
PriorityQueue::PriorityQueue(){
_size = SIZE;
_next = 0;
_pqentry = new pqentry_t*[_size];
}
PriorityQueue::~PriorityQueue(){
delete[] _pqentry;
}
void PriorityQueue::insert(string value, float priority){
_pqentry[_next]->value = value; // this is probably not working
_pqentry[_next]->priority = priority;
_next++;
}
void PriorityQueue::printQueue(){
for (int i = 0; i < _next; i++){
cout << "Value: " << _pqentry[i]->value << endl
<< "Priority: " << _pqentry[i]->priority << endl;
}
cout << endl;
}
main.cpp
#include <iostream>
#include <string>
#include "PriorityQueue.h"
using namespace std;
int main()
{
PriorityQueue pq;
pq.insert("Banana", 23);
pq.printQueue();
}
我想,我知道错误在哪里,在PriorityQueue.cpp,这里:
_pqentry[_next]->value = value;
_pqentry[_next]->priority = priority;
但我不知道出了什么问题,也无法修复。编译器说没有错误。
我希望,你能帮助我。提前致谢!
您确实分配了 _pqentry 成员,但您还需要分配此数组的每个条目,例如:
_pqentry[_next] = 新 pqentry_t;
在写入之前。 并且不要忘记删除那些:)
看起来您正在构造函数中创建一个指向 pqentry_t 的指针数组,但您的插入方法期望它本身是一个 _pqentry 结构数组。您没有为 pqentry_t 元素本身分配 space,因此当您尝试在插入方法中取消引用它们时,程序会崩溃。
尝试将 class 中 _pqentry 的定义更改为 pqentry_t *_pqentry,并将构造函数中的分配更改为新的 pqentry_t[size]。这将允许您的 insert 和 printQueue 方法在写入时访问 _pqentry 的条目。