C++ c++;静态链表

C++ c++;静态链表,c++,class,static,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

我有一个链表类,出于某种原因,当我在其中增加一个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());}
};
intvlist::quietAdd(顶点新建顶点,inti\u位置)
{
Vnode*temp=第一个_节点;
Vnode*newNode=NULL;
//检查是否具有唯一性
如果(查找(新顶点)!=999)
返回0;
//检查非负i值

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

可能是
linked1
linked2
以某种方式指向同一个结构? 你可以试试

printf("adress1 %p", &linked1)
printf("adress2 %p", &linked2)
printf("adress1/size %p", &linked1.size)
printf("adress2/size %p", &linked2.size)
分别针对
Vlist
的其他成员(&linked1.data?)


编辑:现在完整的代码是可见的(并且考虑到add_quiet(…)和add(…)原则上做相同的事情),我不认为是“共享的”大小类字段是问题所在。请使用调试器并跟踪列表的地址。这很奇怪,但我现在比以往任何时候都对解决方案更感兴趣。调试器似乎在出于某种原因声明v2之前就告诉我v2的值是什么。(我使用的是代码块)。
最后,我决定在页面顶部声明这些变量,这个问题已经解决。
我希望发现一些设计缺陷,可以解释一些其他问题,如果不将我的整个项目上传到此网站,这些问题很难描述。没有这样的运气。
至于我节目的其他部分……唉


无论如何,感谢您的帮助。

哪个问题?您通过仅使用一个列表显示代码来演示两个列表中的问题?我们如何提供帮助?发布显示问题的代码,发布您得到的,发布您期望的,而不是概述问题描述。您能发布类的完整定义吗?不相关o你的问题,但是,不要(*this)。size使用this->sizean,不要使用制表符!!!请!!!你能提供Vlist::add()函数定义吗?还有显示你如何比较大小的代码。也许你没有向我们显示的比较代码中有一些逻辑错误。我猜你有“if(v1.getSize)()==v2.getSize)它们看起来不是相同的结构吗,因为v2仍然是空的,除了大小被增加。您在代码::块的基础上使用了什么编译器?我想,g++是您的选择?