编译时,我收到了这个
error: reference to non-static member function must be called
std::sort(sortedCOPG_.begin(),sortedCOPG_.end(),comp_copgNode);
有人能让我像我做错了吗?我尝试了几乎没有类的相同代码,但代码没有问题。它只在我将代码封装在一个类中之后。我在发布之前删除了很多代码,以保持简单。我尝试了其他类似的帖子,但是没有意义。
#include <fstream>
#include <set>
#include <unordered_map>
class Computation {
public:
Computation() {
}
private:
struct copgNode_ {
double weight;
int source;
int target;
};
template <typename Iter>
void mst_score__( Iter first, Iter last) {
Iter current = first;
for (; current != last; ++current) {
for (int i=0; i < n_; ++i) {
if (adjMatrix_[*current][i] != 0 ) sortedCOPG_.push_back(copgNode_{adjMatrix_[*current][i],*current,i});
}
}
//Here, is the line causing errors---
std::sort(sortedCOPG_.begin(),sortedCOPG_.end(),comp_copgNode);
}
void m_init_computation__() {
std::vector<int> mst_test_vec{0,3,7};
mst_score__(mst_test_vec.begin(),mst_test_vec.end());
}
bool comp_copgNode ( const copgNode_& left, const copgNode_& right) {return left.weight < right.weight;}
};
答案 0 :(得分:0)
想出错误,不得不更换比较功能。 因为,它会让sort函数识别自定义比较器会覆盖默认比较器
friend bool operator< ( const copgNode_& left, const copgNode_& right) {return left.weight < right.weight;}