c ++意外静态链表

时间:2009-05-07 11:31:07

标签: c++ class static

我有一个链表类,出于某种原因我在一个中增加一个int 对象例如linked1.size由于某种原因linked2.size也递增!

想法为什么会这样?我没有故意把它变成一个静态变量。

我的代码:

main()
{    
Vlist v1;  
v1.add(1,0);  
v1.add(2,0);  

Vlist v2;
}

增加size成员变量发生在add()函数中,如下所示:

(*this).size++;

结果应该是v1.size == 2和v2.size == 0,而是v2.size == 2!

这个问题让我疯狂了几个小时 - 任何帮助都会非常感激!

添加功能如下:

int Vlist::quietAdd(Vertex new_vertex, int i_loc)
{
Vnode* temp= first_node;
Vnode* newNode= NULL;

//check for unique

if (find(new_vertex)!=999)
return 0;

//check for non-negative i value
if (i_loc<0)
{
    cout<<"Invalid index."<<endl;
    return 0;
}
//and exit here?

else
{
    temp = find(i_loc);

    newNode= new Vnode();

    if (size==0)
        first_node= newNode;

    //assigning to the new vnode the new Vertex value
    (*newNode).updateVertex(new_vertex.getInt());

    //the nxt pointer now points to the value it's replacing or NULL
    (*newNode).updateNextPoint(temp);

    if ((temp==NULL)&&size!=0)
    {
        //size-1 is used to get the pointer to the last value on the list
        (*newNode).updatePrevPoint(find(size-1));
        (*find((size-1))).updateNextPoint(newNode);
    }

    if (temp !=NULL)
    {
        //the new vnode's prev pointer now points to correct location
        (*newNode).updatePrevPoint((*temp).getPrevPoint());

        if ((*temp).getPrevPoint()!=NULL)
            /*the vnode that used to point to the existing vnode now
            points to new vnode*/
            (*((*temp).getPrevPoint())).updateNextPoint(newNode);

        //the old one's prev pointer points back to the new value
        (*temp).updatePrevPoint(newNode);
    }

    /*if we've just put a new vnode at the start then it should be
     pointed to by the "first vnode" pointer*/
    if (i_loc==0)
        first_node=newNode;

    (*this).size++;

}
    return 1;
}

//Vlist class definition
class Vlist
{
private:
    int size;
    Vnode* first_node;

public:
    //copy constructor
    Vlist(const Vlist& vl2):size(vl2.size), first_node(NULL)
    {
        for (int i=0;i<size;i++)
            quietAdd(vl2.read(i),i);
    }

    Vertex getNext();
    Vlist (): size(0)  {   first_node=0;    }
    ~Vlist (){};//make deep!
    bool empty();
    Vnode* find(int i_loc) const;
    int find(Vertex target)const;
    void add(Vertex new_vertex, int i_loc);
    int quietAdd(Vertex new_vertex, int i_loc);
    Vertex remove(int i_loc);

    Vertex read(int i_loc) const;
    Vnode* getFirstNode() {return first_node;}
    int getSize() const { return size;}

    void setSize(int newSize) { size = newSize;}

    char* print() const;
    void delete_List();
};

class Vnode
{
private:
    Vertex vertex;
    Vnode* prev_node;
    Vnode* nxt_node;

public:

    Vnode ()
         : prev_node(0), nxt_node(0)
    {
        vertex.update(0);
    }
    ~Vnode (){}; //destructor
    Vertex getVertex(){return vertex;}

    int getVertexInt() { return vertex.getInt();}
    Vertex getNext(){return (*nxt_node).getVertex();}
    Vnode* getNextPoint()    {   return nxt_node;    }
    Vnode* getPrevPoint()    {   return prev_node;    }
    void updateNextPoint(Vnode* newP) { nxt_node = newP;}
    void updatePrevPoint(Vnode* newP)    {prev_node= newP;}
    void updateVertex(Vertex vertexVal)  {vertex.update(vertexVal.getInt());}
};

3 个答案:

答案 0 :(得分:2)

可能是linked1linked2以某种方式指向相同的结构? 你可以尝试

printf("adress1 %p", &linked1)
printf("adress2 %p", &linked2)
printf("adress1/size %p", &linked1.size)
printf("adress2/size %p", &linked2.size)

对于Vlist的其他成员(&amp; linked1.data?)

编辑:既然完整的代码是可见的(并且给定add_quiet(...)和add(...)原则上做同样的事情)我不认为“共享”大小类字段是问题。使用调试器并跟踪列表的地址。这很奇怪,但我现在比以往任何时候都对解决方案感兴趣

答案 1 :(得分:1)

如果没有看到更多代码,我无法确定,但是v1和v2是否可能实际引用同一个链表对象?

答案 2 :(得分:0)

似乎调试器告诉我v2的值是什么,甚至在它因某种原因被声明之前。 (我正在使用代码块) 最后我决定在页面顶部声明这些变量,这个特殊问题是固定的  我希望发现一些设计缺陷,可以解释一些其他问题,如果不将我的整个项目上传到这个网站,很难描述。没有这样的运气。
  至于我的其他节目...... 叹息

无论如何,谢谢你的帮助。