从给定索引处的向量中删除对象

时间:2013-11-12 11:02:58

标签: c++ object vector

我有几个类,几个向量包含每种类型的对象。为简单起见,我只谈一个。所以我有:

class Multiple : public Question {
    public:
        //Member functions here
    private:
        int num_choices;
        string correct;
        vector<string> choices;
};

它还会继承一些数据成员,int points;int chapter;string prompt;

所以我有vector<Multiple> mcq;来存储类的几个对象(不是动态分配的)。但是现在我需要能够删除给定索引处的对象,我只尝试mcq.erase(index)但是使用Visual Studio 2012它会出现错误Error: no instance of overloaded function "std::vector<_Ty, _Alloc>::erase [with _Ty=Multiple, _Alloc = std::allocator<Multiple>]" matches the argument list argument types are: (int) object type is: std::vector<Multiple, std::allocator<Multiple>>而我不知道它意味着什么或如何解决它。

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:4)

erase方法不是索引而是迭代器位置。你应该这样做:

mcq.erase(mcq.begin() + index)
相关问题