In code looks like this:
comparision algorithm
class PathComp{
public:
virtual bool betterThan(const PathInfo& path1, const PathInfo& path2) const{
//returns true if path1 is shorther than path2
{
};
class with redefined operator ()
class QueueComp{
private:
PathComp* comp;
public:
QueueComp(PathComp* pc);
bool operator () (const pair<PathInfo, string>& item1, const pair<PathInfo, string> item2);
};
QueueComp::QueueComp(PathComp* pc):comp(pc){}
bool QueueComp::operator () (const pair<PathInfo, string>& item1, const pair<PathInfo, string>& item2){
return comp->betterThan(item1.first, item2.first);
}
function using priority queue
list<string> Graph::shortestPath(const string& from, const string& to, PathComp* pc) const{
const QueueComp comp(pc);
std::priority_queue<pair<PathInfo, string>, set<pair<PathInfo, string> >, comp> q;
}
the compilator shows error messages : 'comp' cannot appear in a constant-expression, template argument 3 is invalid, invalid type in declaration before ; token
Does anybody know where's the problem? Thanks for all help.
The compiler already says what the problem is: a non- constexpr
cannot be a template argument. You probably meant to write
std::priority_queue<std::pair<PathInfo, std::string>,
std::vector<std::pair<PathInfo, std::string> >,
QueueComp>
q(comp);
The immediate issue was that comp
is an object but the template expects a type for the comparison function. Once this is fixed the next problem would be that std::set<...>
isn't a viable container to be used with std::priorit_queue<...>
but std::vector<...>
is.
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.