STL sorting issue with GCC

I'm having trouble sorting STL. I am trying to sort an object vector by a data member in an object. I looked through a few examples, but as soon as it gets into my configuration, it somehow does not compile under GCC. I tested Visual Studio and it works. I get this error in GCC:

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

I do not understand that the same code will be compiled in Visual Studio.

Here is my setup.

driver.cpp

DB t1;
t1.print();
cout << "---sorting---" << endl;
t1.sSort();
t1.print();

DB class

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());
}

and my stock class only has data items.

Is there any way around GCC?

I believe my question is similar to this , but the solutions there do not work for me.

+3
source share
2 answers

operator()() const-.

bool operator()(const stock& a, const stock& b) const

, stock::getSymbol() const. , , operator()() , (const).

+6

- , g++ STL , const. ()

bool operator()(const stock &a, const stock &b)

, .

+1

Source: https://habr.com/ru/post/1772774/


All Articles