【发布时间】:2015-09-02 18:09:39
【问题描述】:
我正在尝试使用 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;
}
但是,我在编译时遇到了愚蠢的错误:
“错误:'void'和'void'类型的无效操作数到二进制'operatorgetVal() getVal();"
有人可以帮我吗?
【问题讨论】:
-
您的
getVal返回void。也许应该是int? -
... 并实际返回值而不仅仅是打印它。
标签: c++ stable-sort