与GCC签订STL排序问题

tho*_*ang 3 c++ sorting vector

我一直有STL排序的问题.我正在尝试通过对象中的数据成员对对象矢量进行排序.我查了几个例子,但一旦它落入我的配置,它就不能在GCC下编译.我在Visual Studio上测试过它的工作原理.我在GCC上遇到这个错误:

no match for call to '(test::by_sym) (const stock&, const stock&)

我不明白的是,相同的代码将在Visual Studio上编译.

这是我的设置.

driver.cpp

DB t1;
t1.print();
cout << "---sorting---" << endl;
t1.sSort();
t1.print();
Run Code Online (Sandbox Code Playgroud)

class DB

vector<stock> list;

struct by_sym {
bool operator()(stock &a, stock &b)  {
return a.getSymbol() < b.getSymbol();
}
};

void DB::sSort(){
std::sort(list.begin(), list.end(), by_sym());
}
Run Code Online (Sandbox Code Playgroud)

而我的股票类只有数据成员.

GCC有解决方法吗?

我相信我的问题与类似,但那里的解决方案对我不起作用.

wil*_*ell 6

operator()()的const不正确.将其更改为

bool operator()(const stock& a, const stock& b) const
Run Code Online (Sandbox Code Playgroud)

确保stock::getSymbol()也是一个const功能.如果不是,你不能改变它,那么operator()()取值的参数,而不是(const)参考.