STL vectors and push_back

This is a discussion on STL vectors and push_back within the C++ Programming forums, part of the General Programming Boards category; I can't seem to put anything in my vector. Let me get into specifics : I defined class Mesh somewhere ...

  1. #1
    Lode Runner
    Join Date
    May 2004
    Posts
    53

    STL vectors and push_back

    I can't seem to put anything in my vector. Let me get into specifics :

    I defined class Mesh somewhere else in my code.
    I wish for a Model class to contain Meshes. This is the relevent part of my code.
    Code:
    Model::Model(int n) {
    	numOfObjects = n;
    	objects = vector<Mesh*>(n);
    }
    
    void Model::addObject(Mesh *newObject) {
    	objects.push_back(newObject);
    }
    Strangely, my objects vector seems not to fill up. In the debugger, when I call upon push_back(), I can see that the _M_start of objects changes (to 0x113e8a0 for example) but that it still references to NULL (0x0) and I can see that my object has not been added to the vector. And then come the Bus Errors.
    I can also see in the debugger that my newObject argument is a well-formed object, exactly the one I want.

    What's my problem ? Without any help, it'll soon be schizophrenia.
    Last edited by krappa; 11-13-2004 at 10:18 AM.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    What's my problem ?
    Not showing enough code (such as at least the declartion of the member variables)
    Not using the initalizer list in your constructors
    Not showing enough code
    Not asking the right questions
    Not showing enough code
    Making a dumb reference to a mental illness when asking for help
    Not showing enough code

  3. #3
    Lode Runner
    Join Date
    May 2004
    Posts
    53
    Sorry, I thought my constructor would be enough to guess the variables. And I don't like to overload my posts with code.

    Code:
    class Model {
    public:
    	Model(int n);
    	void addObject(Mesh *newObject);
    	void drawModel();
    	char *description();
    private:
    	int numOfObjects;
    	vector<Mesh*> objects;
    };
    I hope this is enough. Please help. And try to refrain from sarcastic comments, it's not like they're doing any good.

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    I suggest you post the code calling the functions including the instantiation of the objects probably in main().

  5. #5
    Lode Runner
    Join Date
    May 2004
    Posts
    53
    my main is mostly a test function for now. I declare a global variable :
    Code:
    Model * model;
    then call upon the static method
    Code:
    ASELoader::loadASE(model,argv[1]);
    in which the model is given a better value through my constructor
    Code:
    model = new Model(objectCount);
    Further in the loadASE method, I call the method :
    Code:
    model->addObject(newMesh);
    I know, thanks to the debugger, that newMesh is the object I expect and is correctly passed to the addObject method. But my objects vector doesn't insert it. This is the first time I use STL vectors so I'm pretty lost.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Wouldn't it be easier to just keep a vector of objects instead of pointers? Also you really should use the initilzer list for the variable objects.

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Did you add a using directive?
    using std::vector;
    Did you include vector

    I imagine you did otherwise other errors would pop?

    I'm sorry I can't be of more help. Like thantos said there doesn't seem to be enough information to go on. You probably have more experience than me anyway.

  8. #8
    Lode Runner
    Join Date
    May 2004
    Posts
    53
    yep I thought of that. #include <vector>, using namespace std. it's all there.

  9. #9
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Code:
    Model::Model(int n) {
    	numOfObjects = n;
    	objects = vector<Mesh*>(n);
    }
    You are creating a vector of n empty (NULL) objects. Also, numOfObjects and objects.size() seem to hold the same data, so you can get rid of numOfObjects

    You don't have to explicitly maintain the count. The vector will do that for you, so you can get rid of numOfObjects, and even get rid of your constructor (don't pass a size), and then the default constructor will do what you want it to.

    In your current code, the first object you add will be at objects[n], where n is the number you passed to the Model constructor.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  10. #10
    Lode Runner
    Join Date
    May 2004
    Posts
    53
    great ! It worked.

    Thanks a bunch.

    Just for the sake of curiosity, what case would you use the
    Code:
    vector<TYPE>(int)
    constructor in ?

  11. #11
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    When you know the size before-hand (you know you will have n values) and you're ok with them all being default initialized, it is best to use that form. An example is a vector of ints that keeps track of the number of times each alphabetic character appears in a file. You know that vector will have 26 values (one for each letter), and you want to start off the values at 0 for each letter, so you would use:

    std::vector<int> letterCount(26);

Popular pages Recent additions subscribe to a feed

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