简体   繁体   中英

C++ STL vector.erase() always deletes last element

Using: Eclipse 3.8.1, C/C++ Remote Debug Launcher 6.0.0, with GCC 1.1.0, and GDB 7.0.0 Verified on: Visual Studio 2010

I am pulling my hair out on this one. When using the following code, the last element of the vector of Room objects rooms is always removed. I cannot get any element other than the last to erase with this code. I am only trying to remove one element (the location is determined by the user).

I know that many of you are probably asking why I don't use a list, but random data access is much more important than ease of element addition / removal with this collection. Do you have any ideas?

void House::removeRoom(unsigned int roomToRemove){
    try{
        if(roomDoesNotExist(roomToRemove)) throw houseException("Room requested to     remove does not exist", roomToRemove, __FILE__, __LINE__);

        vector<Room>::iterator roomIterator = rooms.begin() + roomToRemove;
        rooms.erase(roomIterator);

        removeAllLinksToRoom(roomToRemove);
        renumberLinkedRoomsAfterErase(roomToRemove);
    }
    catch(houseException& hException){
        hException.display(cerr);
    }
}

Here is a snippet of the House class:

#include "Room.h"

using namespace std;

class House {
public:
    vector<Room> rooms;

    //member functions
    void removeRoom(unsigned int roomToRemove);


    //constuctors / destructors
    House();
    virtual ~House();

    //STL linked list overload operators =, ==, and <
    House &operator=(const House &rhs);
    int operator==(const House &rhs) const;
    int operator<(const House &rhs) const;

private:
    bool roomDoesNotExist(int roomToRemove);
    void removeAllLinksToRoom(int roomToUnlink);
    void renumberLinkedRoomsAfterErase(int erasedRoom);
};

Because it could be relevant, I included my entire Room class:

#include "Wall.h"

#include <vector>
#include <algorithm>
#include <string>

#include "exceptions/houseException.h"

using namespace std;

class Room {
    public:
    float setPointDegF;
    vector<Wall> walls;  //TODO consider making walls protected / private

    private:
    string roomName;
    vector<int> linkedRooms;
    float storedTemperature;
    float storedHumidity;

    //member functions
    void linkToRoom(int roomToLink);
    void unlinkFromRoom(int roomToUnlink);

    void removeAllLinksToRoom(int roomToUnlink);
    void renumberLinkedRoomsAfterErase(int erasedRoom);

public:
    //friends
    friend class House;

    //member functions
    void addWalls(unsigned int numWallsToAdd=1);
    void removeWall(unsigned int wallToRemove);

    //Sensor Functions
    void readSensorTemperature();
    void readSensorHumidity();

    void temperature();
    void humidity();

    //constuctors / destructors
    Room();
    virtual ~Room();
//STL linked list overload operators =, ==, and <
    Room &operator=(const Room &rhs);
    int operator==(const Room &rhs) const;
    int operator<(const Room &rhs) const;
private:
    void getAttachedRooms(Wall& tempWall);

    bool wallDoesNotExist(unsigned int wallToRemove);
    bool roomLinked(int roomToLink);
    bool roomNotLinked(int roomToLink);

    vector<int>::iterator findRoom(int roomToFind);
    vector<int>::iterator findInsertionPoint(int roomToInsert);


};

rooms.erase(roomIterator) will delete the element at location roomIterator . If you want to delete all elements from beginning to roomToRemove , use rooms.erase(rooms.begin(), rooms.begin()+roomToRemove) or rooms.erase(rooms.begin(), roomIterator)

nm answered this in the comments below my question, but I am retyping it here for clarity:

Also, does the Room class have an assignment operator and a copy constructor defined correctly? – nm

Answer: No. An explicit Room class assignment operator was causing the issue.

I tried to overload the default assignment operator and failed miserably. When I commented out that code and used the implicit &= operator, life is good. Problem solved. -neghzero

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