Ich habe eine verknüpfte List-Klasse und aus irgendeinem Grund, wenn ich ein int in einem Objekt erhöhen. Z. B. linked1.size aus irgendeinem Grund linked2.size erhöht auch!C++ versehentlich statische verknüpfte Liste
Und Ideen, warum das ist? Ich habe es nicht absichtlich zu einer statischen Variable gemacht.
mein Code:
main()
{
Vlist v1;
v1.add(1,0);
v1.add(2,0);
Vlist v2;
}
die Größe Membervariable Inkrementieren tritt in der add()
Funktion wie folgt aus:
(*this).size++;
Das Ergebnis sollte die v1.size == 2 und v2.size = sein = 0, aber stattdessen v2.size == 2!
Dieses Problem hat mich stundenlang verrückt gemacht - jede Hilfe würde wirklich geschätzt werden!
Die Zusatzfunktion ist wie folgt:
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());}
};
Welches Problem? Sie demonstrieren ein Problem mit zwei Listen, indem Sie Code mit nur einem Code anzeigen? Wie können wir helfen? Veröffentlichen Sie den Code, der das Problem aufweist, und posten Sie, was Sie erhalten haben, und geben Sie anstelle einer allgemeinen Problembeschreibung die erwarteten Ergebnisse ein. –
Können Sie die vollständige Definition Ihrer Klasse veröffentlichen? – PaulJWilliams
Nicht verwandt mit Ihrer Frage, aber statt (* this) .size verwenden Sie diese-> Größe –