Thread: Vector of Objects = crash and burn

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    9

    Vector of Objects = crash and burn

    I want to...
    1) make a simple class Object that stores some integers etc...
    2) put this Object class into a vector
    3) Loop through the vector and call the class member function stats() which prints info the Object is storing.

    I have tested Object and it should work fine my problem is getting classes into the vector and then dereferencing the function stats. Any help greatly appreciated I don't think this should be that difficult, but I don't know the answer.

    Code:
    #include<iostream>
    #include<vector>
    using namespace std;
    
    class Object
    {
      public:
        int mCurPosX;
        int mCurPosY;
        
        int mNewPosX;
        int mNewPosY;
        
        int mSymbol;  //integer that will be cast to a character when printed
        
        static int sNumObjects;
        
        Object(int curPosX, int curPosY, int symbol);
        void stats();
    };
    
    int Object::sNumObjects = 0;
    
    Object::Object(int curPosX, int curPosY, int symbol)
    {
        mCurPosX = curPosX;
        mCurPosY = curPosY;
        mSymbol = symbol;
        
        mNewPosX = mCurPosX;
        mNewPosY = mCurPosY; 
        
        ++sNumObjects;   
    }
    
    void Object::stats()
    {
        cout << "=========== Object " << this << " Stats ===========\n";
        cout << "mCurPosX = " <<  mCurPosX << endl;
        cout << "mCurPosY = " <<  mCurPosY << endl;
        cout << "mSymbol = " << mSymbol << "\n\n";
        
        cout << "mNewPosX = " <<  mNewPosX << endl;
        cout << "mNewPosY = " <<  mNewPosY << "\n\n";
        
        cout << "Total Number of Objects = "<< sNumObjects << endl;
    }
    
    
    //#########################################
    //                          PROBLEM AREA BELOW
    //#########################################
    
    int main()
    {   
        vector<Object*> pVectorObj;
        
        for(int i=0; i<=10; ++i)
        {
            Object* temp = new Object(0,0,97);
            pVectorObj.push_back(temp);
        }
            
    
    
        for(vector<Object*>::const_iterator iter = pVectorObj.begin();
            iter != pVectorObj.end(); ++iter)
        {
            cout << "iter = " << *iter << endl;
    
            //produces error iter->.stats();                        //#############
            //error: stats not declared *iter.stats();
            //error: stats not declared *(*iter).stats();
    
        }
        
        cout << "pVectorObj.size() = " << pVectorObj.size() << endl;
        
        
        system("pause");
        return 0;   
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you're holding pointers in your vector, you must dereference the iterator and the pointer. I would do it this way:
    Code:
    (*iter)->stats();
    However, I don't see a reason to hold pointers in your vector. Why not just use vector<Object>? If you made that switch then you wouldn't have to worry about potential memory leaks like the one your program has now.
    Last edited by Daved; 04-14-2008 at 02:49 PM.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    9

    Talking Thanks! ! !

    I was really banging my head on that one. The reason I was trying to use pointers was because (I didn't get the Object vector to work either), and also my book that I am learning from used pointers instead of a vector of objects.

    Thanks again!

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I didn't get the Object vector to work either.
    I'd highly recommend focusing on getting that to work:
    Code:
        for(int i=0; i<=10; ++i)
        {
            pVectorObj.push_back(Object(0,0,97));
        }
    
        // ...
    
            iter->stats();

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    9
    I wasn't able to get it to work. This is what main looks like (I haven't changed the class Object). Also something else I don't understand is that it prints stats() now no matter what and it isn't in the constructor.

    Code:
    int main()
    {   
        vector<Object> vectorObj;
        char userInput = 'n';
        
        for(int i=0; i<=10; ++i)
        {
            vectorObj.push_back( Object(i,i,97));
        }
        
        cout << "Print stats from vectorObj? y n :" << endl;
        cin >> userInput;
        
        if( userInput == 'y')
        {
            for(vector<Object>::const_iterator iter = vectorObj.begin();
                iter != vectorObj.end(); ++iter)
            {
                
                iter->stats();
                // passing `const Object' as `this' argument of 
                // `void Object::stats()' discards qualifiers 
                        
            }
        }
    
        cout << "vectorObj.size() = " << vectorObj.size() << endl;
        
        
        system("pause");
        return 0;   
    }

  6. #6
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    I suspect part of your problem lies here:
    Code:
     for(vector<Object*>::const_iterator iter = pVectorObj.begin();
    You must be getting compiler errors, what are they telling you?

    Hint: Pressing the delete key 6 times within that line will do the trick.

    EDIT: I should offer the alternative as well:

    adding "const" to stats() in it's definition and prototype will work as well:
    Code:
    class Object{
    
        void stats() const;
    
        //...
    };
    
    void Object::stats() const{
    
    }
    You need to either make stats() return constant, if you want a constant object to call it, or just use a regular iterator and leave stats() alone:
    Code:
    class Object{
    
      void stats();
      //...
    };
    
    void Object::stats(){
    
    }
    
    int main(){
    
        vector<Object> my_vec;
        //...
        vector<Object>::iterator iter = my_vec.begin();    // NOTE: The use of regular iterator here
        //...
        return 0;
    }
    Last edited by dudeomanodude; 04-14-2008 at 10:26 PM.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

Popular pages Recent additions subscribe to a feed