vectors and classes

This is a discussion on vectors and classes within the C++ Programming forums, part of the General Programming Boards category; Is there some sort of limitations of what kind must the datatype be if I create a vector from it? ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    129

    vectors and classes

    Is there some sort of limitations of what kind must the datatype be if I create a vector from it? I made my own string class and now when I try to push_back a new string to my empty vector I get general protection fault while running.

    Any guesses what might possibly cause this?
    kooma - t.h@iki.fi

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    129
    Here's the .cpp for my class (s & l are private - all functions public).
    Attached Files Attached Files
    kooma - t.h@iki.fi

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Vectors don't restrict the data type that they contain, the vector class is very general on that point so that we can use vectors in place of arrays. Chances are the problem is in how you are using the vector that causes the fault.

    Try stepping through your code to find the exact operation that causes the fault and see what all values are at that point. You can pinpoint the problem and develop a fix for it easily this way.

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

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    I can't remember exactly, but I think I remember seeing somewhere that the vector template creates a copy of the data that gets inserted into it. What this means is that if your class does not have copy constructor defined for it, then the compiler generates a default one that may not be suitable for your needs. This could be causing the problem. Try making a copy constructor for your class and see if the problem goes away. Then again, this might not be the reason why things are failing.
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector of vectors containing classes
    By larne in forum C++ Programming
    Replies: 3
    Last Post: 01-13-2009, 06:19 AM
  2. classes and vectors
    By izuael in forum C++ Programming
    Replies: 10
    Last Post: 11-27-2006, 03:19 PM
  3. Vectors and custom classes
    By cunnus88 in forum C++ Programming
    Replies: 16
    Last Post: 05-12-2006, 05:11 AM
  4. vectors and classes
    By jimothygu in forum C++ Programming
    Replies: 3
    Last Post: 04-27-2003, 07:53 PM
  5. How To use vectors for custom classes
    By johnnyd in forum C++ Programming
    Replies: 14
    Last Post: 03-25-2003, 09:04 PM

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