0

So I have this code:

#include <vector>

using namespace std;

class A 
{
   private:
       int int_prop;
       virtual void someFunc() = 0;
   public:
       void setIntProp(int p) { int_prop = p; }
       int getIntProp() { return int_prop; }
};

class B : public A 
{
       void someFunc() {}
};

int main ()
{
    // initialize vector for the purpose of this demo
    vector<A*> vecA;
    std::auto_ptr<B> obj1(new B);
    vecA.push_back(&*obj1);
    ( *vecA.front() ).setIntProp(15); // set int_prop of 1st item
    std::auto_ptr<B> obj2(new B);
    vecA.push_back(&*obj2);
    ( *vecA.back() ).setIntProp(4); // set int_prop of 2nd item 

    vector<A*>::iterator a_it[N]; // this is the iterator array  
                                             //for vecA
    int k = 0;

    for (vector<A*>::iterator it = vecA.begin(); it != vecA.end(); it++)
    {
       if ( (*it)->getIntProp() >= 10 )
       {
          a_it[k] = it; // store this element
          k++; // increment array index
       } // end if
    }

    // finally delete the stored iterators
    for (int i = 0; i<k; i++)
    {
       vecA.erase(a_it[i]);
    }

    return 0;
}

It should be noted that I don't want to duplicate the vector, I just want to store some of its' elements for deletion, because just deleting the elements of the vector while iterating it causes runtime errors.

More to the point I get these errors:

 Error  1   error C2057: expected constant expression   
   Error    2   error C2466: cannot allocate an array of constant size 0    
   Error    3   error C2133: 'a_it' : unknown size  
    4   IntelliSense: expression must have a constant value 

that point to this line of code:

vector<A*>::iterator a_it[vecA.size()];

EDIT 1~ I should also mention that the condition to remove an item from the vector is based on the value of an int value of property int_prop of the class as shown in the added code (Note: this is the reason I haven't found my answer in the other Q's)

Any help would be appreciated. Thanks

Mechanic
  • 172
  • 1
  • 10

0 Answers0