0

i have have a class with a set of object pointers and i want to save theme in sorted order by their some field(id). for debugging i simplified that to this:

#include<iostream>
#include<set>
using namespace std;
struct c{
    auto comp=[](int* i1,int* i2){return *i1>*i2;};
    set<int*,decltype(comp)> seti(comp);
};
int main(){}

i searched here to find how to sort them and i select the second approach(because i cant use c++20) when you try to use that in main function or out of a class/structure it works. but i want to use it in my class and i didn't know how to get

auto comp=[](int* i1,int* i2){return *i1>*i2;};

in class.i don't want to using a global lambda.

EDIT: with constexpr static auto comp=[](int* i1,int* i2){return *i1>*i2;}; compiler get this error:

m.cpp:4:27: error: ‘constexpr const c::<lambda(int*, int*)> c::comp’, declared using local type ‘const c::<lambda(int*, int*)>’, is used but never defined [-fpermissive]
     constexpr static auto comp=[](int* i1,int* i2){return *i1>*i2;};
  • I hate it when this happens. At `set seti(x);` you've fooled the poor compiler into thinking you're declaring a function, not defining a variable. changing the braces up on the initialization should get things fixed up. `set seti{x};` note the `{}` in place of the `()`. – user4581301 Dec 09 '20 at 01:27
  • For the first part, see if `constexpr static auto x=[](int* i1,int* i2){return *i1>*i2;};` inside the class definition does what you need it to do – user4581301 Dec 09 '20 at 01:36
  • Found the duplicate. Would have been faster to write the ing thing. – user4581301 Dec 09 '20 at 01:39
  • Have you tried the suggested fix, i.e. `(comp)` --> `{comp}`? Fix that, and if you still have an error, edit the question to just focus on that. – cigien Dec 09 '20 at 02:30
  • @cigien yes i edited the post tnks for recal that – alireza haghi Dec 09 '20 at 02:38
  • Thanks for editing the answer. However, don't just mention that the first fix works. Actually edit your code, and remove the first error message from the question. – cigien Dec 09 '20 at 02:40
  • @user4581301 i tried with constexpr... it doesn't works. – alireza haghi Dec 09 '20 at 02:45
  • It looks like you're compiling with an older version. You need C++17 for this. – cigien Dec 09 '20 at 03:06
  • 2
    @cigien yes i compiled it with --std=c++11 and now it compiled by --std=c++17 without error tanks again – alireza haghi Dec 09 '20 at 03:23
  • I'm sorry I left that Standard requirement out. Totally slipped my mind. – user4581301 Dec 09 '20 at 04:00

0 Answers0