How To use vectors for custom classes

This is a discussion on How To use vectors for custom classes within the C++ Programming forums, part of the General Programming Boards category; Hey folks. Question: How do you implement Vectors for a user defined class? Is this even possible? I've searched the ...

  1. #1
    Registered User johnnyd's Avatar
    Join Date
    Mar 2002
    Posts
    70

    Question How To use vectors for custom classes

    Hey folks. Question: How do you implement Vectors for a user defined class? Is this even possible? I've searched the boards, the faq and the tutorials and all of them only refer to using the predefined C++ datatypes. What about custom data types or classes? Could someone please shine some light on this for me? Thanks.
    Excuse me, while I water my money tree.

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    It's possible. It should be the same as any other data type.

  3. #3
    Registered User johnnyd's Avatar
    Join Date
    Mar 2002
    Posts
    70
    I agree, it should. It's just that it doesn't work when I use my own custom defined types. I was wondering if there was something else that I needed to do. Hence, the post. Any ideas?
    Excuse me, while I water my money tree.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >How do you implement Vectors for a user defined class?
    I'm not quite sure what you mean, are you trying to create a vector of objects?
    Code:
    #include <iostream>
    #include <vector>
    
    class obj
    {
    public:
      void show ( int i )
      {
        std::cout<<"index "<< i <<" working!"<<std::endl;
      }
    };
    
    int main()
    {
      std::vector<obj> v ( 5 );
    
      for ( std::vector<int>::size_type i = 0; i < v.size(); i++ )
        v[i].show ( i );
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User johnnyd's Avatar
    Join Date
    Mar 2002
    Posts
    70
    Yes, a vector of objects. You know it's interesting, that's exactly how I defined my vector of objects. Check this:

    Code:
    vector<Book> vBook;
    Which is supposed to be a vector of books. However, when I compile, I get this error:

    Error: memory.h(194,33):Could not find a match for 'Book::Book(const Book)'
    which doesn't make sense to me. I was thinking that I was doing the wrong thing. I have made vectors of predefined data types like int, float, char* etc... But I can never get it to work for user defined data types or objects. So I was thinking it's not possible. What am I doing wrong?

    Thanks again.

    [Edit]
    I'm using Borland C++ Builder 5.02 btw.
    Excuse me, while I water my money tree.

  6. #6
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    The vector class assumes that you have done certain things in your class. If you want to put a bunch of your objects in a vector, you must have the following:
    • a default constructor that takes no arguments
    • a copy constructor ex: book(const &book b);
    • an overloaded assignment operator

    The error message you showed sounds like your missing the copy constructor.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  7. #7
    Registered User johnnyd's Avatar
    Join Date
    Mar 2002
    Posts
    70
    You see, it's things like that which annoy me. They [the text] tell you how to do everything except the most important thing. Now how was I supposed to know that vectors for user defined objects required an overloaded assignment operator? I would never have thought of that. What's odd joshdick is that I already have a copy constructor in the class. Go figure.

    Is it just me or does everyone build their classes with all these things checked off? I must be weird or something. Thanks for the enlightenment joshdick. You were most helpful.

    [EDIT]
    Prelude - sorry I wasn't clearer in the earlier post. I'm still recovering after my hiatus with C++ binary file handling (ask Codeplug). This learning experience is not working for me.
    Last edited by johnnyd; 03-23-2003 at 11:44 PM.
    Excuse me, while I water my money tree.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is it just me or does everyone build their classes with all these things checked off?
    Yes, most people have their classes designed and debugged before ever writing a bit of code (or they should). Most problems in the implementation stem from lack of proper design.

    -Prelude
    My best code is written with the delete key.

  9. #9
    Registered User johnnyd's Avatar
    Join Date
    Mar 2002
    Posts
    70
    No, what I meant was, do most people build their classes with the assignment operator overloaded and have a copy constructor? The reason I ask is because the text (and most other sources) do not mention that those things are required for defining a vector for a user defined class.

    In other words, when building just about any class (regardless of its simplicity), do you always create copy constructors and overloaded assignment operators? Is it common practice like how you'd have private: and public: members?

    I'm trying to get at the fact that these things were assumed in the text and other sources. Can u see my point? Someone new to C++ would never have known that if they didn't directly ask someone. That's what I'm getting at.
    Excuse me, while I water my money tree.

  10. #10
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Joshdick, are those requirements compiler-specific? I have no problems creating vector of objects in MSVC++ that have NONE of those requirements you specified.

    Of course, if you try to do things that require one of those, then you will get an error. For example:
    Code:
    #include<vector>
    using namespace std;
    
    class myclass
    {
    public:
       myclass(int X) : x(X) {};
       void doSomething() { return;};
    private:
       int x;
    };
    
    int main()
    {
       vector<myclass> vec1;  // compiles just fine
       vector<myclass> vec2(5);  // error!  can't create the 5 objects since no default constructor
       return 0;
    }
    Johnnyd, what exactly is the line that the error is on?

  11. #11
    Registered User johnnyd's Avatar
    Join Date
    Mar 2002
    Posts
    70
    I'm glad you posted PJYelton, because from your code:

    Code:
    vector<myclass> vec1;
    would give me the compile error I mentioned earlier. Whereas, in your case, where MS VC++6 you said, it compiles fine. Again, I'm using Borland C++ 5.02.

    So it is pretty clear to me then this problem is obviously compiler specific. I guess I cannot rely on just any source where this is concerned. I guess that's why most sources don't include the fact that you need an overloaded assignment operator and a copy constructor. Cuz I was wondering, who'd need to define those for every class if they're not going to be used that way?
    Excuse me, while I water my money tree.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I guess that's why most sources don't include the fact that you need an overloaded assignment operator and a copy constructor.
    You don't need either of these unless the default copying (member-wise copy) is insufficient, such as when you have pointer members.

    >Again, I'm using Borland C++ 5.02.
    You haven't given us the code that is giving you problems. Seeing as how you keep getting errors that we can't duplicate, it is difficult to tell what is going wrong. The problem as you have described is not a problem on any of my compilers, including Borland 5.5. So, post the code that is giving you problems and we can solve this more quickly than a thread full of educated guesses.

    -Prelude
    My best code is written with the delete key.

  13. #13
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Here's what SAMS Teach Yourself C++ in 21 Days says about vectors.

    The default constructor ... is called to create a new element. This provides another argument in favor of explicitly defining a default constructor for your own classes. If you do not, you cannot use the standard vector container to hold a set of instances of your class.

    For this function (push_back) to work, your class must define a copy constructor. Otherwise, this push_back() function will not be able to make a copy of your object.

    DO define an overloaded assigment operator for a class if its instances are likely to be held in a vector.
    And yes, I do go through every class I write to ensure that I have made a default constructor, a copy constructor, and have overloaded the assignment operator.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  14. #14
    Registered User johnnyd's Avatar
    Join Date
    Mar 2002
    Posts
    70

    Thumbs up

    Thanks all. Problem solved. ( You guys are good )
    Excuse me, while I water my money tree.

  15. #15
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Thank you. Man, it really feels good to help people :-)
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors of classes
    By Dondrei in forum C++ Programming
    Replies: 26
    Last Post: 03-20-2009, 06:11 AM
  2. vector of vectors containing classes
    By larne in forum C++ Programming
    Replies: 3
    Last Post: 01-13-2009, 07:19 AM
  3. classes and vectors
    By izuael in forum C++ Programming
    Replies: 10
    Last Post: 11-27-2006, 04:19 PM
  4. Vectors and custom classes
    By cunnus88 in forum C++ Programming
    Replies: 16
    Last Post: 05-12-2006, 06:11 AM
  5. Vectors + Classes = Confusing
    By Epo in forum C++ Programming
    Replies: 59
    Last Post: 12-18-2004, 04:42 PM

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