myVector class - erasing
myVector class - erasing
我需要把std::vector放到模板类中。除了擦除
之外,一切正常。#include <vector>
using namespace std;
template <class TBase>
class TCollection
{
protected:
//The Vector container that will hold the collection of Items
vector<TBase> m_items;
public:
int Add(void)
{
//Create a new base item
TBase BaseItem;
//Add the item to the container
m_items.push_back(BaseItem);
//Return the position of the item within the container.
//Zero Based
return (m_items.size()-1);
}
//Function to return the memory address of a specific Item
TBase* GetAddress(int ItemKey)
{
return &(m_items[ItemKey]);
}
//Remove a specific Item from the collection
void Remove(int ItemKey)
{
//Remove the Item using the vector’s erase function
m_items.erase(GetAddress(ItemKey));
}
void Clear(void) //Clear the collection
{
m_items.clear();
}
//Return the number of items in collection
int Count(void)
{
return m_items.size(); //One Based
}
//Operator Returning a reference to TBase
TBase& operator [](int ItemKey)
{
return m_items[ItemKey];
}
};
我得到错误:
1>b:projectsc++wolvesislandconsoleapplication6consoleapplication6myvector.h(24): error C2664: 'std::_Vector_iterator<_Myvec> std:: vector_iterator <_Myvec>::erase(std::_Vector_const_iterator<_Myvec>)':无法将参数1从'obiekt **'转换为'std::_Vector_const_iterator<_Myvec>'
我试图删除的方式:data.Remove(2);其中数据为myVector
erase
方法只接受迭代器,不接受指针作为参数。看这里。
可能的修正是
std::vector<TBase>::const_iterator it = m_items.begin() + ItemKey;
m_items.erase(it);
虽然我没有亲自测试
vector erase函数只接受迭代器。
vec.erase(vec.begin()+ItemKey);
相关文章:
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- C++核心准则 C35 对于接口类"A base class destructor should be either public and virtual, or protected and nonv
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- "Undefined class"作为is_base_of的论据
- 如何将class.cpp和class.hpp编译为一个.o文件
- '{'标记之前的预期类名,然后在预声明时无效使用不完整的类型'class class_name'
- 如何使用"derived input class"创建派生类?
- 错误:"Left of getValue must have class/struct/union"
- ' class a : b ' 和 ' class a : public b ' 之间的继承类不同
- Pybind11 Class Definition
- 在C++中使用没有合作伙伴Class/Cpp文件的头文件是否实用
- C2011 'CMemDC':Visual Studio 2019中的'class'类型重新定义
- TypeError: [c++ addon class] 不是构造函数
- C++ class template
- 当初始值设定项是基类名时'initializer does not name a non-static data member or base class'错误
- Clearing Class Foo with new(pFoo) Foo()
- C++ class vs a library
- 继承类时"invalid use of incomplete type ‘class tree_node_t’"
- 使用"class"关键字,后跟未声明的标识符
- myVector class - erasing