简体   繁体   中英

C++ iterator as class member used in class method

i have the following code.

The for-loop at the end should go through the object of CCarList class, print out the the a_rz and vin of Car structure and stop when the AtEnd() method returns true.

But it doesnt stop and moreover when i try to reach the values of a_rz and vin it gives segmentation fault.

Could someone, please, explain how to use properly iterator in my CCarList class?

Thanks

    typedef struct Car {

        string a_rz;
        unsigned int vin;
    }Car;

    class CCarList
     {
       public:
        string         RZ           ( void ) const;
        unsigned int   VIN          ( void ) const;
        bool           AtEnd        ( void ) const;
        void           Next         ( void );

        vector<Car*> vCar;
        vector<Car*>::const_iterator it = vCar.begin();
       public:
                       CCarList     ( void ){}
                       ~CCarList    ( void ){}
     };

     string CCarList::RZ ( void ) const {
        return "ahoj"; //(**it).a_rz;
     }
    unsigned int CCarList::VIN ( void ) const{
        return 5; //(**it).vin;
    }
    bool CCarList::AtEnd ( void ) const {

        if(it == vCar.end()) return true;
        return false;
    }
    void  CCarList::Next ( void ){
        it++;
    }

    int main() {


    Car  *a, *b, *c;
    a = new Car;
    b = new Car;
    c = new Car;

    (*a).a_rz = "abc";
    (*a).vin = 45;
    (*b).a_rz = "dfg";
    (*b).vin = 65;
    (*c).a_rz = "jkl";
    (*c).vin = 23;

    CCarList list_of_cars;
    list_of_cars.vCar.push_back(a);
    list_of_cars.vCar.push_back(b);
    list_of_cars.vCar.push_back(c);

    for ( ; ! list_of_cars . AtEnd (); list_of_cars . Next () )
         cout << list_of_cars . RZ () << ", " << list_of_cars . VIN () << endl;


    return 0;
    }

Your problem is that iterator it is not being updated/invalidated after each push_back . After last insertion it still points to "nothing" as it was from beginning.

Soultion is simple -- update your iterator. Add a method for adding new elements:

void CCarList::Add(Car* car) 
{
    vCar.push_back(car);
    it = vCar.begin();
}

and then just:

list_of_cars.Add(a);
list_of_cars.Add(b);
list_of_cars.Add(c);

Also regarding above problem, you're trying to wrap vector and provide same functionality that vector already provides. Consider moving functionality related to Car structure inside that structure. And leaving in CCarList only methods that are related to CCarList . Just a short piece of code to show you what I mean:

typedef struct Car {
    string a_rz;
    unsigned int vin;
} Car;

class CCarList {
public:
    vector<Car*> vCar;
    CCarList(void){}
    ~CCarList(void){}
};

int main() {

    Car  *a, *b, *c;
    a = new Car;
    b = new Car;
    c = new Car;

    a->a_rz = "abc";
    a->vin = 45;
    b->a_rz = "dfg";
    a->vin = 65;
    c->a_rz = "jkl";
    c->vin = 23;

    CCarList list_of_cars;

    list_of_cars.vCar.push_back(a);
    list_of_cars.vCar.push_back(b);
    list_of_cars.vCar.push_back(c);

    for(auto car : list_of_cars.vCar)
        cout << car->a_rz << ", " << car->vin << endl;

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM