+ -
当前位置:首页 → 问答吧 → 一个c++迭代器初始化,赋值,比较的问题

一个c++迭代器初始化,赋值,比较的问题

时间:2010-07-17

来源:互联网

本帖最后由 lishizelibin 于 2010-07-17 14:54 编辑
  1. void CAsDisplayObject::up(int entity)
  2. {
  3.     if (!entity)
  4.         return;

  5.     std::stack<dob_iterator> nodes;
  6.         nodes.push(0);
  7.     dob_iterator inode = m_nodes.begin();
  8.     dob_iterator isuper = 0;
  9.     dob_iterator istart = 0;
  10.     dob_iterator iend = 0;

  11.     for (; inode != m_nodes.end(); inode++)
  12.     {
  13.         int ent = (*inode)->m_entity;

  14.         if (!ent)
  15.             continue;

  16.         // store possible super node
  17.                 if ((nodes.top() == 0) ||
  18.             (*nodes.top())->m_entity != ent)
  19.         {
  20.             // new entity node started
  21.             if (nodes.top() != 0 &&
  22.                 ent == entity)
  23.             {
  24.                 isuper = nodes.top();
  25.                 istart = inode;
  26.             }

  27.             nodes.push(inode);
  28.         }
  29.         else
  30.         {
  31.             // entity node finished
  32.             if (ent == entity)
  33.             {
  34.                 iend = ++inode;
  35.                 inode--;
  36.             }
  37.             if (isuper != 0 &&
  38.                 (*isuper)->m_entity == ent)
  39.             {
  40.                 if (istart != 0 && iend != 0)
  41.                 {
  42.                     if (iend != inode)
  43.                         m_nodes.splice(inode, m_nodes, istart, iend);
  44.                 }
  45.                 isuper = 0;
  46.                                 istart = 0;
  47.                                 iend = 0;
  48.             }
  49.             nodes.pop();
  50.         }
  51.     }
  52. }
复制代码
问题:
error C2664: “std::stack<_Ty>::push”: 不能将参数 1 从“int”转换为“const std::list<_Ty>::_Iterator<_Secure_validation> &”
error C2440: “初始化”: 无法从“int”转换为“std::list<_Ty>::_Iterator<_Secure_validation>”
error C2678: 二进制“==”: 没有找到接受“std::list<_Ty>::_Iterator<_Secure_validation>”类型的左操作数的运算符(或没有可接受的转换)
。。。
运行环境:VS2005

作者: lishizelibin   发布时间: 2010-07-17

std::stack<dob_iterator> nodes;
nodes.push(0); // 这个0代表什么呢?

其实之后的 dob_iterator isuper = 0; 等等我也不知道你是什么意思?
估计是一种无聊的置零情节。即使是“置零情节”,这个“零”也只是一种泛指,并不是指“0”

作者: bruceteen   发布时间: 2010-07-17



QUOTE:
std::stack nodes;
nodes.push(0); // 这个0代表什么呢?

其实之后的 dob_iterator isuper = 0; 等等我 ...
bruceteen 发表于 2010-07-17 16:40




    这个在2003中可以通过,不过我觉的,不知道该怎么去初始化和比较,我再找找相关list操作,也麻烦楼上高手能指教一二,小弟不甚感激

作者: lishizelibin   发布时间: 2010-07-17

VC2003中可能 dob_iterator 就是一个指针类型,所以它可以做 = 0 操作
但我估计很少有库厂家会这么设计,所以你说的VC2005就把dob_iterator改为了一个类。

dob_iterator isuper; 本身就是有值的,不应该用 dob_iterator isuper = 0;
即使你想显式的赋初值,那也应该是 dob_iterator isuper = dob_iterator();

作者: bruceteen   发布时间: 2010-07-17

回复 bruceteen


    你的建议很中肯,谢谢,我先研究一下。

作者: lishizelibin   发布时间: 2010-07-17

ww

作者: 123   发布时间: 2010-08-12

void CAsDisplayObject::up(int entity)
{
    if (!entity)
        return;

    std::stack<dob_iterator> nodes;
        //nodes.push(NULL);
    dob_iterator inode = m_nodes.begin();
    dob_iterator isuper = dob_iterator();
    dob_iterator istart = dob_iterator();
    dob_iterator iend= dob_iterator();

    for (; inode != m_nodes.end(); inode++)
    {
        int ent = (*inode)->m_entity;

        if (!ent)
            continue;

        // store possible super node
        if ((nodes.top() == dob_iterator()) ||
            (*nodes.top())->m_entity != ent)
        {
            // new entity node started
            if (nodes.top() != dob_iterator() &&
                ent == entity)
            {
                isuper = nodes.top();
                istart = inode;
            }

            nodes.push(inode);
        }
        else
        {
            // entity node finished
            if (ent == entity)
            {
                iend = ++inode;
                inode--;
            }
            if (isuper != dob_iterator() &&
                (*isuper)->m_entity == ent)
            {
                if (istart != dob_iterator() && iend != dob_iterator())
                {
                    if (iend != inode)
                        m_nodes.splice(inode, m_nodes, istart, iend);
                }
                isuper = istart = iend = dob_iterator();
            }
            nodes.pop();
        }
    }
}
这样可以编译过,

作者: intellicad代码研究论坛   发布时间: 2010-08-12