我试图使用stable_sort对指针向量进行排序
到某个班级去。我有这样的密码:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class B
{
public :
B(int y, int j) {x = y, r = j;};
void getVal() {cout << x << endl; };
int x;
int r;
};
bool compareB(B* b1, B* b2)
{
return b1->getVal() < b2->getVal();
}
int main()
{
B b1(3, 4), b2(-5, 7), b3(12, 111);
vector<B*> myVec;
myVec.push_back(&b1);
myVec.push_back(&b2);
myVec.push_back(&b3);
std::stable_sort(myVec.begin(), myVec.end(), compareB);
for (size_t size = 0; size < myVec.size(); ++size)
{
myVec[size]->getVal();
}
return 0;
}但是,在编译它时,我会得到一个错误:
错误:对二进制的'operator<‘返回b1->getVal() getVal()的类型'void’和'void‘的无效操作数;
有人能帮我吗?
发布于 2015-09-02 18:13:29
问题在于
void getVal() {cout << x << endl; };它返回void而不是某些值。
当您在return b1->getVal() < b2->getVal();中使用它时,它可以归结为不编译的return void < void;。
您应该能够将其更改为
int getVal() { return x; };https://stackoverflow.com/questions/32360266
复制相似问题