У меня есть связанный класс списка, и по какой-то причине, когда я увеличиваю int в одном объекте , например, link1.size по какой-то причине linked2.size также увеличивает!C++ случайно статический связанный список
И идеи, почему это так? Я не намеренно делал это статической переменной.
мой код:
main()
{
Vlist v1;
v1.add(1,0);
v1.add(2,0);
Vlist v2;
}
инкремента переменной-члена размера происходит в 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());}
};
Какая проблема? Вы демонстрируете проблему с двумя списками, показывая код, используя только один? Как мы можем помочь? Отправьте код, который показывает проблему, и опубликуйте, что вы получаете, и опубликуйте то, что вы ожидали, вместо описания проблемы с обзором. –
Можете ли вы опубликовать полное определение своего класса? – PaulJWilliams
Не относится к вашему вопросу, но вместо (* this) .size используйте это-> размер –