错误 C2664 - 代码在 VC6 中编译正常; VS 2010 中没有
我有一个 typedef,一个带有使用该类型的成员向量的类,然后是一个使用 std::
#typedef DWORD WordNo_t;
class CWordList : public CObject
{
public:
WordNo_t* begin() { return m_Words.begin(); }
WordNo_t* end() { return m_Words.end(); }
void truncate (WordNo_t *Ptr)
{
if (Ptr == end())
return;
ASSERT (Ptr >= begin() && Ptr < end());
// following line generates C2664
m_Words.erase (Ptr, end());
}
private:
std:vector<WordNo_t> m_Words;
}
详细错误为:
错误 C2664: 'std::_Vector_iterator<_Myvec>; std::vector<_Ty>::erase(std::_Vector_const_iterator<_Myvec>,std::_Vector_const_iterator<_Myvec>)' :无法将参数 1 从 'const WordNo_t' 转换为 'std::_Vector_const_iterator<_Myvec>'
对 STL 来说还很陌生......任何帮助将不胜感激。
I have a typedef, a class with a member vector using that type and then a method using std::<vector>::erase().
#typedef DWORD WordNo_t;
class CWordList : public CObject
{
public:
WordNo_t* begin() { return m_Words.begin(); }
WordNo_t* end() { return m_Words.end(); }
void truncate (WordNo_t *Ptr)
{
if (Ptr == end())
return;
ASSERT (Ptr >= begin() && Ptr < end());
// following line generates C2664
m_Words.erase (Ptr, end());
}
private:
std:vector<WordNo_t> m_Words;
}
The detailed error is:
error C2664: 'std::_Vector_iterator<_Myvec> std::vector<_Ty>::erase(std::_Vector_const_iterator<_Myvec>,std::_Vector_const_iterator<_Myvec>)' : cannot convert parameter 1 from 'const WordNo_t' to 'std::_Vector_const_iterator<_Myvec>'
Pretty new to STL... Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我很惊讶
begin
和end
甚至能够编译,它们不应该编译。std::vector
(和朋友)使用迭代器,而不是指针。 (尽管它们的作用类似。)无论如何,
erase
采用迭代器,而不是指针。因为向量是连续的,所以您可以像这样创建实用函数:基本上,找出
&pVec[0]
(第一个元素)中有多少个元素pPtr
,然后将其添加到pVec.begin()
中。 (将指针和指向起始位置的偏移量转换为从起始位置开始的偏移量。)此操作的复杂度为 O(1)。进而:I'm surprised
begin
andend
are even compiling, they shouldn't.std::vector
(and friends) use iterators, not pointers. (Though they are intended to act similarly.)In any case,
erase
takes an iterator, not a pointer. Because vectors are contiguous, you can make utility functions as such, though:Basically, find out how many elements
pPtr
is from&pVec[0]
(the first element), then add that topVec.begin()
. (Transform the offset from a pointer and the pointer to start into the offset from the start.) This operation is O(1). And then:指针不是迭代器。
erase
接受一个迭代器,但您传递给它一个指针。也许您应该更改truncate
以也采用迭代器?A pointer is not an iterator.
erase
takes an iterator but you are passing it a pointer. Perhaps you should changetruncate
to also take an iterator?