On Thu, 2016-12-22, Joseph Hesse wrote:
> The documentation of std::set says that the second optional argument of
> a template set instantion should be a function object or a function pointer.
> In the example below I am trying to create a set object using a function
> pointer.
> I compiled the code with:
> $ g++ -std=c++11 Set.cpp
You may want to add some or all of '-Wall -Wextra -pedantic' to that
command line.
> and got the compiler error that there was a type mismatch in the second
> argument.
> Please help.
> Thank you,
> Joe
>
> ========================= Set.cpp =============================
> #include <iostream>
> #include <set>
>
> class X
> {
> private:
> int x;
> public:
> X(int a) : x(a) {}
> friend bool Comp(const X &, const X &);
> };
>
> bool Comp(const X &xobj1, const X &xobj2)
> {
> return xobj1.x < xobj2.x;
> }
>
> int main()
> {
> std::set<X, Comp> SetOfX; // <== use function pointer
>
> return 0;
> }
It's not what you ask for, but I find the simplest thing is to,
whenever possible:
std::set<X> SetOfX;
and define operator< for X.
/Jorgen
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/
snipabacken.se> O o .