STL set insert help
Anyone help me out with why the following fails?
int main(int argc, char *argv)
cout << "mylist.size() = " << mylist.size() << endl;
Compile error ...
g++ -o main main.cpp -I../includes
main.cpp: In function ‘int main(int, char**)’:
main.cpp:30:5: error: return-statement with no value, in function returning ‘int’
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include/g++-v4/string:50:0,
/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include/g++-v4/bits/stl_function.h: In member function ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = Node]’:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include/g++-v4/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 = Node, _Val = Node, _KeyOfValue = std::_Identity<Node>, _Compare = std::less<Node>, _Alloc = std::allocator<Node>]’
/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include/g++-v4/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 = Node, _Compare = std::less<Node>, _Alloc = std::allocator<Node>, typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator = std::_Rb_tree_const_iterator<Node>, value_type = Node]’
main.cpp:26:20: instantiated from here
/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include/g++-v4/bits/stl_function.h:230:22: error: no match for ‘operator<’ in ‘__x < __y’
make: *** [main] Error 1
Firstly, you need to return 0 (or some integer) from main.
Secondly, you need to define a comparison function for your Node type.
Read up on sets.
Yes, a set is an ordered container. As such, they need to know how to order whatever it is you put into them. For basic types such as int, float, etc. there are already well established rules for what one value is greater or less than some other value. For user defined types such as your Node object the only way the compiler can tell how to order things is if you provide an appropriate method of doing so. Without such methods for a given user defined type, code using a set will not compile. By default, such container order themselves according to the less-than operator (operator<) although this can be overridden upon instantiation of the set to use other orderings. So, at a minimum, a defined less-than operator should allow your code to compile successfully:
bool operator<(const Node& lhs, const Node& rhs)
// Put code here to return true/false value based on whether lhs is less than rhs
No you don't. That's optional.
Originally Posted by oogabooga
You entirely missed the point.
Originally Posted by Elysia
The point was that currently it was just a "return;" which is why there was an error.
He perhaps doesn't really need to know yet, that there is a special case for main where the entire statement is optional.
Eh. Good point. I missed that. My mistake :(