Thread: compile time error

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    134

    compile time error

    What is the error here which is throwing exception at compile time?
    Code:
    #include <iostream>
    #include <set>
    #include <vector>
    
    using namespace std;
    struct edge{
            int n2;
            char wt;
    };
    typedef struct edge edge;
    
    
    int main(){
            vector<set<edge> > v;
            edge e;
    
            e.n2 = 1;
            e.wt = 'a';
            v[1].insert(e);
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    A set is an ordered container. That order, by default, is governed by use of the less-than operator (operator<). For the code to compile, the set needs to have a defined less-than operator that compares one edge object with another.

    The typedef is unnecessary and you've also got a problem assuming v[1] exists (it doesn't) when you try to insert an edge object into the set at that index.
    "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

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    134
    Quote Originally Posted by hk_mp5kpdw View Post
    A set is an ordered container. That order, by default, is governed by use of the less-than operator (operator<).
    What do you mean by this, kindly elaborate.

    Quote Originally Posted by hk_mp5kpdw View Post
    For the code to compile, the set needs to have a defined less-than operator that compares one edge object with another.
    How to do that?
    Quote Originally Posted by hk_mp5kpdw View Post
    The typedef is unnecessary
    why?
    Quote Originally Posted by hk_mp5kpdw View Post
    you've also got a problem assuming v[1] exists (it doesn't) when you try to insert an edge object into the set at that index.
    How to make sure it exists, but I don't want to effect the size of vector too. May be its not possible, just to confirm!

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by kapil1089thekin View Post
    What do you mean by this, kindly elaborate.
    Items inserted into a set container are ordered according to the less-than operator (by default). If this operator is not defined already for the type of data the set is to contain, then the code cannot compile. Inserting the values 5 9 12 3 8 into a set<int> container will result in the set containing the following: 3 5 8 9 12. This is because a less-than operator already exists for integer types and new integer values in the set will be ordered from least to greatest as they are inserted. For custom types, in your case the edge object, there is no readily available one... you have to provide it for the compiler to produce the necessary code to ensure the set can properly order the items that get inserted into it.


    Quote Originally Posted by kapil1089thekin View Post
    How to do that?
    Create a function such as the one below:
    Code:
    bool operator<(const edge& lhs, const edge& rhs)
    {
      // Put code in here that compares the two edge objects and return a true/false value
    }
    Quote Originally Posted by kapil1089thekin View Post
    why?
    In C++ when you create a struct, you can simply refer to the struct by its name after that point. You can simply say "edge" instead of "struct edge" or using a typedef like you might want to if you were using C. The typedef isn't hurting anything, it's simply redundant here.

    Quote Originally Posted by kapil1089thekin View Post
    How to make sure it exists, but I don't want to effect the size of vector too. May be its not possible, just to confirm!
    Easiest way for the purpose of getting it to simply compile and run would be to set things up so that there were at least 2 elements in the vector container to begin with. This would be as easy as changing the code where v is defined to:
    Code:
    vector<set<edge> > v(2);
    This creates a vector with 2 elements, v[0] and v[1] (each of those with an empty set<edge> container). Your code would then work.
    "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

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    134
    Thanks for your clear answer, hats off!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. using c++ in c code
    By hannibar in forum C Programming
    Replies: 17
    Last Post: 10-28-2005, 09:09 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM