2014-06-12 43 views
1

我想通过传递第一个和最后一个项目并传递一个布尔谓词来排序向量。不能为我的生活弄清楚我做错了什么。怀疑它对课程缺乏了解。但它真的让我难住。排序不喜欢我的谓词

我正在通过加速的C++,并在第4章。我在阅读时花了一段时间,但没有到任何地方。可能不帮助我,我在课堂上做事情,所以我可以并排保留章节。

我得到的错误是

error C2064: term does not evaluate to a function taking 2 arguments 

上调用排序行。所以在一个头文件(Ch4)中,我有以下内容(为了您的理智,我编辑了这些代码 - 包含内容等似乎很高兴 - 头文件和代码位于单独的文件中,并且正在使用vs2013,因此它的排序全部这为我)

class Ch4 
{ 
public: 
    int Ch4::Run(); 
    struct Student_Info; 
    bool compare(const Student_Info& x, const Student_Info& y); 
} 

然后在类:

struct Ch4::Student_Info 
{ 
    string name; 
    double midterm, final; 
    vector<double> homework; 
}; 

int Ch4::Run() 
{ 
    vector<Student_Info> students; 
    ... code that populates it 
    sort(students.begin(), students.end(), compare); 
} 

bool Ch4::compare(const Student_Info& x, const Student_Info& y) 
{ 
    return x.name < y.name; 
} 

当我把上面一行

sort(students.begin(), students.end(), Ch4::compare); 

我收到一个错误,说它缺少一个参数列表 - 但在我的重载列表中没有显示一个参数列表。

所以我按照它的意见,并将其用作参考,我回到原来的错误信息。

所以我感到困惑的两两件事: 1)为什么我的代码工作 - 如何解决 2)什么是这些错误消息告诉我,为什么他们似乎谈论没有按”过载存在还是对我隐藏?

+0

申报比较功能为静态。 –

+0

它必须是* functor * - 或者是一个函数指针,一个lambda或者一个重载'operator()'的类型。 –

回答

1

compare是一个非静态方法

让它static

static bool compare(const Student_Info& x, const Student_Info& y); 
1

问题是您的compare函数是非静态的。您可能需要将其设置为一个自由函数,否则将其设置为静态。