Requirements for using a user-defined class in STL

This is a discussion on Requirements for using a user-defined class in STL within the C++ Programming forums, part of the General Programming Boards category; I am trying to use a class that I created with std::list, but VC++ gives me errors such as: error ...

  1. #1
    Registered User NixPhoeni's Avatar
    Join Date
    Aug 2001
    Posts
    10

    Question Requirements for using a user-defined class in STL

    I am trying to use a class that I created with std::list, but VC++ gives me errors such as:
    error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class std::list<struct my_struct,class std::allocator<struct my_struct> >::const_iterator' (or there is no acceptable conversion)

    The same error for !=.

    So I'm assuming that my_struct needs something so that I can use it with STL. I know that a constructor, copy constructor, assignment, and bool operators == and < are necessary. Is there anything else? I can't remember where I found it the first time and I can't search the boards for some strange reason.
    Any help or a url is appreciated.
    -Joe
    -------------------------------------------
    To understand a program you must become both the machine and the program.
    --Alan Perlis

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    If you'd post some code we could take a look at it for you.

  3. #3
    Registered User kitten's Avatar
    Join Date
    Aug 2001
    Posts
    109
    You have to define = operator for you struct.
    Making error is human, but for messing things thoroughly it takes a computer

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    23
    That's the beuty of C++ and the STL - if it doesn't compile, you missed a requirement. look at your compiler output, and correct your class accordingly.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    43
    Maybe you should use * before the iterator, e.g.

    mylist::value_type value;
    for (mylist::const_iterator i = list.begin(); i != mylist.end(); ++i)
    {
    value = i;//Error of your type
    if (value != i) //same error
    {//.....
    }
    value = *i; //Correct if you have a propper copy constructor
    // (or default one if possible)
    }

  6. #6
    Registered User NixPhoeni's Avatar
    Join Date
    Aug 2001
    Posts
    10

    Exclamation To clear things up...

    I just wanted someone to post the requirements for using a user-defined class in a STL class so I could fix my problem. I'm pretty positive I'm missing something, I just don't know what it is. If anybody knows all of these requirements (I listed a few I knew in my initial post), _please_ post them! Thanks...


    -Joe

  7. #7
    Registered User NixPhoeni's Avatar
    Join Date
    Aug 2001
    Posts
    10

    Lightbulb Here's the code...

    Ok, found the site that lists the requirements if anyone's interested:
    http://www.msoe.edu/eecs/cese/resources/stl/objreq.htm


    And the offending code that produced all of the error messages in my first message at the locations with "<---":

    std::list <my_struct>::iterator dp;
    dp = c.affects.begin(); <---

    while(!affects.empty()) affects.pop_front();

    while(dp!=c.affects.end()) <---
    ...

    This is in a member function (operator =) for a class that contains a variable of type std::list <my_struct> named affects.
    my_struct is a struct with a default constructor, copy constructor, destructor, operator =, operator ==, and operator <.
    If anybody can help (or needs clarification before he or she can) please let me know. I would be eternally grateful because I don't know what is going wrong here!
    -Joe
    -------------------------------------------
    To understand a program you must become both the machine and the program.
    --Alan Perlis

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    23
    If you posted the compiler error - it was a great help.

    My guess is that maybe c or c.affects are const, and therefor, their iterator is const_iterator.

    But please send the exact compiler error (and compiler type) and I'll try to see if I can see the problem.

  9. #9
    Registered User NixPhoeni's Avatar
    Join Date
    Aug 2001
    Posts
    10

    Talking Yes!

    Shmulik you were right on the button! I thought people had forgotten about this thread. I had actually tried what you had suggested after inspecting the error message more carefully. I had to replace the ::iterator with ::const_iterator. I wonder if that's just because it was in a member function referring to a member of that class (affects, in this case). Well, anyways, thanks!



    -Joe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. STL Map Problem with user defined Class
    By noobcpp in forum C++ Programming
    Replies: 21
    Last Post: 07-21-2008, 12:02 PM
  2. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  3. win32 and user defined classes
    By paulf in forum Windows Programming
    Replies: 4
    Last Post: 04-16-2002, 06:12 PM
  4. Header files
    By borland_man in forum C++ Programming
    Replies: 14
    Last Post: 02-22-2002, 03:30 AM
  5. Difficulty superclassing EDIT window class
    By cDir in forum Windows Programming
    Replies: 7
    Last Post: 02-21-2002, 04:06 PM

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