I am working with std::priority_queue for the first time for a University assignment. The assignment is a simulation of process scheduling. I would like to pass a parameter to my Comparison struct constructor to initialise and I thought I saw it on another forum, but I'm unable to find the source again. I looked on SO before posting but I didn't see anything similar.
Here is my priority_queue:
/* schedules.hpp / .cpp */
#include "process.hpp"
namespace my = procschedassignment;
int tick = 0;
std::priority_queue<my::Process, _
std::vector<my::Process>,
PrioritiseHighestResponseRatioNext(tick) > rq;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
line 100 - compiler errors are here
// ...
Here is my Comparison struct:
/* prioritise_process.hpp / .cpp */
#include "process.hpp"
namespace my = procschedassignment;
struct PrioritiseHighestResponseRatioNext {
public:
explicit PrioritiseHighestResponseRatioNext(int const& cpu_time)
: cpu_time_(cpu_time) {};
bool PrioritiseHighestResponseRatioNext::operator()(my::Process const& lhs,
my::Process const& rhs) {
bool ret;
if (lhs.wait_time() > cpu_time_) {
ret = (lhs.ResponseRatio() > rhs.ResponseRatio());
} else if (rhs.wait_time() > cpu_time_) {
ret = (lhs.ResponseRatio() < rhs.ResponseRatio());
}
return ret;
};
private:
const int cpu_time_;
};
The compiler errors I get with this code are:
../src/schedules.cpp:100: error: ‘time’ cannot appear in a constant-expression
../src/schedules.cpp:100: error: template argument 3 is invalid
../src/schedules.cpp:100: error: invalid type in declaration before ‘;’ token
Is it possible to have a parameterised Comparison struct with std::priority_queue? I am new to STL so I apologise that I don't have a better understanding of what's happening here.
You are trying to pass an object as template-parameter. This will not work. You should provide your comparator as argument to the constructor, and the type of the comparator as template-argument.
// declare type
typedef std::priority_queue<my::Process,
std::vector<my::Process>,
PrioritiseHighestResponseRatioNext > process_queue;
// ^^^ just a type, no object ^^^
// create object
process_queue rq(PrioritiseHighestResponseRatioNext(tick));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.