错误 C2664 - 代码在 VC6 中编译正常; VS 2010 中没有

发布于 2024-09-11 06:09:24 字数 800 浏览 2 评论 0原文

我有一个 typedef,一个带有使用该类型的成员向量的类,然后是一个使用 std::::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;
}

详细错误为:
错误 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扬花落满肩 2024-09-18 06:09:24

我很惊讶 beginend 甚至能够编译,它们不应该编译。 std::vector (和朋友)使用迭代器,而不是指针。 (尽管它们的作用类似。)

无论如何,erase 采用迭代器,而不是指针。因为向量是连续的,所以您可以像这样创建实用函数:

template <typename T, typename A>
typename std::vector<T, A>::iterator
to_iterator(T* pPtr, std::vector<T, A>& pVec)
{
    ASSERT(pPtr >= &pVec.front() && pPtr <= &pVec.back());

    return pVec.begin() + (pPtr- &pVec[0]);
}

template <typename T, typename A>
typename std::vector<T, A>::const_iterator
to_iterator(const T* pPtr, const std::vector<T, A>& pVec)
{
    ASSERT(pPtr >= &pVec.front() && pPtr <= &pVec.back());

    return pVec.begin() + (pPtr - &pVec[0]);
}

基本上,找出 &pVec[0] (第一个元素)中有多少个元素 pPtr ,然后将其添加到 pVec.begin() 中。 (将指针和指向起始位置的偏移量转换为从起始位置开始的偏移量。)此操作的复杂度为 O(1)。进而:

void truncate (WordNo_t *Ptr)
{
    // note the == end() bit will be in here anyway:
    m_Words.erase(to_iterator(Ptr, m_Words), end());
}

I'm surprised begin and end 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:

template <typename T, typename A>
typename std::vector<T, A>::iterator
to_iterator(T* pPtr, std::vector<T, A>& pVec)
{
    ASSERT(pPtr >= &pVec.front() && pPtr <= &pVec.back());

    return pVec.begin() + (pPtr- &pVec[0]);
}

template <typename T, typename A>
typename std::vector<T, A>::const_iterator
to_iterator(const T* pPtr, const std::vector<T, A>& pVec)
{
    ASSERT(pPtr >= &pVec.front() && pPtr <= &pVec.back());

    return pVec.begin() + (pPtr - &pVec[0]);
}

Basically, find out how many elements pPtr is from &pVec[0] (the first element), then add that to pVec.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:

void truncate (WordNo_t *Ptr)
{
    // note the == end() bit will be in here anyway:
    m_Words.erase(to_iterator(Ptr, m_Words), end());
}
后知后觉 2024-09-18 06:09:24

指针不是迭代器。 erase 接受一个迭代器,但您传递给它一个指针。也许您应该更改 truncate 以也采用迭代器?

A pointer is not an iterator. erase takes an iterator but you are passing it a pointer. Perhaps you should change truncate to also take an iterator?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文