[英]Pointer to Pointer to Structure (Priority Queue)
I'm a beginner (in C++, I'm coming from C (6 months experience)) and I'm trying to create a priority queue, but something is not working. 我是一个初学者(在C ++中,我来自C(已有6个月的经验)),我正在尝试创建优先级队列,但是有些问题不起作用。 When I start the program and compile it, there are no errors. 当我启动程序并进行编译时,没有错误。 But there is nothing printed on the screen and the program crashes. 但是屏幕上没有任何打印内容,程序崩溃。
So here is the code: 所以这是代码:
PriorityQueue.h 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 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 main.cpp
#include <iostream>
#include <string>
#include "PriorityQueue.h"
using namespace std;
int main()
{
PriorityQueue pq;
pq.insert("Banana", 23);
pq.printQueue();
}
I think, I know where the error is, in the PriorityQueue.cpp, here: 我想,我知道错误在哪里,在PriorityQueue.cpp中,这里:
_pqentry[_next]->value = value;
_pqentry[_next]->priority = priority;
But I don't know what's wrong and I can't fix it. 但是我不知道出了什么问题,也无法解决。 The compiler says there are no errors. 编译器说没有错误。
I hope, you can help me. 我希望你能帮帮我。 Thanks in advance! 提前致谢!
You did allocate the _pqentry member but you need to allocate each entry of this array as well, for example: 您确实分配了_pqentry成员,但还需要分配该数组的每个条目,例如:
_pqentry[_next] = new pqentry_t; _pqentry [_next] =新的pqentry_t;
before writing to it. 在写之前。 And do not forget to delete those :) 并且不要忘记删除那些:)
It looks like you are creating an array of pointers to pqentry_t in your constructor, but your insert method is expecting it to be an array of _pqentry structures themselves. 看起来您在构造函数中创建了指向pqentry_t的指针数组,但是您的insert方法期望它是_pqentry结构本身的数组。 You are not allocating space for the pqentry_t elements themselves and so when you try to dereference them in your insert method, the program crashes. 您没有为pqentry_t元素本身分配空间,因此当您尝试在insert方法中取消引用它们时,程序崩溃。
Try changing the definition of _pqentry in the class to pqentry_t *_pqentry and the allocation in the constructor to new pqentry_t[size]. 尝试将类中_pqentry的定义更改为pqentry_t * _pqentry,并将构造函数中的分配更改为新的pqentry_t [size]。 This will allow your insert and printQueue methods to access the entries of _pqentry as they are written. 这将使您的insert和printQueue方法可以在写入_pqentry时访问它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.