Like Tree1Likes
  • 1 Post By oogabooga

STL set insert help

This is a discussion on STL set insert help within the C++ Programming forums, part of the General Programming Boards category; Anyone help me out with why the following fails? Code: int main(int argc, char *argv[]) { Node n(1,1); set<Node> mylist; ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    14

    STL set insert help

    Anyone help me out with why the following fails?

    Code:
    int main(int argc, char *argv[])
    {
        Node n(1,1);
        set<Node> mylist;
       
        mylist.insert(n);
       
        cout << "mylist.size() = " << mylist.size() << endl;
       
        return;
    }

    Compile error ...
    Code:
    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,
                     from main.cpp:10:
    /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

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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.
    iMalc likes this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    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:
    Code:
    bool operator<(const Node& lhs, const Node& rhs)
    {
        // Put code here to return true/false value based on whether lhs is less than rhs
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,756
    Quote Originally Posted by oogabooga View Post
    Firstly, you need to return 0 (or some integer) from main.
    No you don't. That's optional.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by Elysia View Post
    No you don't. That's optional.
    You entirely missed the point.
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,756
    Eh. Good point. I missed that. My mistake

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. insert map into map?
    By zxcv in forum C++ Programming
    Replies: 2
    Last Post: 09-05-2011, 01:20 PM
  2. insert
    By nirnir26 in forum C Programming
    Replies: 6
    Last Post: 11-26-2009, 07:14 AM
  3. std::insert
    By Coding in forum C++ Programming
    Replies: 7
    Last Post: 07-10-2008, 02:08 PM
  4. insert another exe into exe
    By lliero in forum C Programming
    Replies: 8
    Last Post: 04-12-2002, 12:22 PM
  5. Replies: 1
    Last Post: 09-17-2001, 05:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21