set of struct

Aug 22, 2012 at 1:45pm
Hello I am trying to make a set of struct in c++ but I am having errors, and I could not make it.

I made 2 options.

First one:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct pointsSet {
	double p1;
	double p2;
	double p3;
};

void foo()
{
std::set<pointsSet> setBoundaryPoints;

double p[3] = {1,2,3};
pointsSet *pt = new pointsSet;
pt->p1 = p[0];
pt->p2 = p[1];
pt->p3 = p[2];

setBoundaryPoints.insert(*pt);
}


I have this error , the last line is the important I think:


/usr/include/c++/4.5/bits/stl_function.h: In member function ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = rfsim::pointsSet]’:
/usr/include/c++/4.5/bits/stl_tree.h:1184:4:   instantiated from ‘std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key = rfsim::pointsSet, _Val = rfsim::pointsSet, _KeyOfValue = std::_Identity<rfsim::pointsSet>, _Compare = std::less<rfsim::pointsSet>, _Alloc = std::allocator<rfsim::pointsSet>]’
/usr/include/c++/4.5/bits/stl_set.h:408:29:   instantiated from ‘std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = rfsim::pointsSet, _Compare = std::less<rfsim::pointsSet>, _Alloc = std::allocator<rfsim::pointsSet>, typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator = std::_Rb_tree_const_iterator<rfsim::pointsSet>, value_type = rfsim::pointsSet]’
/home/amadio/Softs/Src/TrajectoryPlanning/Plugins/TrajectoryPlanning.planningplugin/src/Mesh.cpp:3278:31:   instantiated from here
/usr/include/c++/4.5/bits/stl_function.h:230:22: error: no match for ‘operator<’ in ‘__x < __y’





The second one is :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct pointsSet {
   double p1;
   double p2;
   double p3;
   pointsSet(double a, double b, double c): p1(a), p2(b), p3(c) {};
};
	
void foo()
{
std::set<pointsSet> setBoundaryPoints;

double p[3] = {1,2,3};

//This one gives me :
// error: ‘pt’ was not declared in this scope
setBoundaryPoints.insert(pt(p[1],p[2],p[3]));


//If i put this:
//The same error as before, that operator < is not defined.
struct pointsSet pt(p[1],p[2],p[3]);
setBoundaryPoints.insert(pt);
}



Some help?.. thanks!
Last edited on Aug 22, 2012 at 1:45pm
Aug 22, 2012 at 1:53pm
error: no match for ‘operator<’

Well, you need to provide an operator< for the type you wish to use in a set (or use your own predicate)

1
2
3
4
bool operator<(const pointsSet& lhs, const pointsSet& rhs)
{
    return std::tie(lhs.p1, lhs,p2, lhs.p3) < std::tie(rhs.p1, rhs.p2, rhs.p3);
}


online demo: http://ideone.com/qCTVD

(also, don't use new)
Aug 22, 2012 at 2:40pm
Hey, thank you for the repply!

I try to make that, and have this error
 /home/amadio/Softs/Src/TrajectoryPlanning/Plugins/TrajectoryPlanning.planningplugin/src/Mesh.cpp:3557:12: error: ‘tie’ is not a member of ‘std’ 


Looking for that, i found this :

http://www.cplusplus.com/reference/std/tuple/tie/

I think is easier , insted of making a set of structure.

But it helps.



But it would be nice to solve the problem I had, so the topic remains closed and solved =)
Topic archived. No new replies allowed.