I'm new to c and am encountering this error while building the code. Found some answers online but am unable to understand them. Can someone please explain what this error means and how to resolve it in simple terms?
For reference, I'm attaching the code snippets. These snippets are part of a project, so might miss out on a few details.
file.cpp
#include "File.h"
using namespace std;
pQueue::pQueue()
{
std::priority_queue<std::pair<int, Category*>> q;
}
pQueue::~pQueue()
{
}
void queueWork(std::priority_queue<std::pair<int, Category*>> q, int action, Category* data)
{
q.emplace(make_pair(action, data));
}
void doWork(std::priority_queue<std::pair<int, Category*>> q)
{
while (true)
{
std::pair<int, Category*> queueElement;
queueElement = q.top();
Category* data = queueElement.second;
// Perform some operation, get return value
q.pop();
}
}
file.h
#pragma once
#include <queue>
#include <thread>
class pQueue
{
public:
pQueue();
~pQueue();
void queueWork(std::priority_queue<std::pair<int, Category*>> q, int action, Category* data);
private:
void doWork(std::priority_queue<std::pair<int, Category*>> q);
};
main.cpp
#include "File.h"
bool function(Category* data)
{
if (data)
{
// Will set action values later
auto action = 1;
pQueue::queueWork(Queue, action, data);
// TODO: Implement getResult function to get the return value
}
return true;
}
CodePudding user response:
I would use the rule of zero here (see rule of 3 and rule of 5)
class pQueue {
public:
void queueWor(int action, Category* data);
private:
void doWork();
std::priority_queue<std::pair<int, Category*>> q;
};
The compiler generated destructor, copy, and move will be correct and sufficient in this case.
Note that in your current implementation q
is not a member variable, instead it is simply a local variable that only exists within the scope of your constructor.