J'ai une classe de liste liée et pour une raison quelconque lorsque j'incrémente un int dans un objet par exemple linked1.size pour une raison quelconque, linked2.size est également incrémenté!C++ liste liée accidentellement statique
Et pourquoi c'est? Je n'ai pas intentionnellement fait une variable statique.
mon code:
main()
{
Vlist v1;
v1.add(1,0);
v1.add(2,0);
Vlist v2;
}
incrémenter la variable membre de taille se produit dans la fonction add()
comme ceci:
(*this).size++;
Le résultat devrait être le v1.size == 2 et v2.size = = 0, mais à la place v2.size == 2!
Ce problème m'a rendu fou pendant des heures - toute aide serait vraiment appréciée!
La fonction add est la suivante:
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());}
};
Quel problème? Vous démontrez un problème avec deux listes en montrant le code en utilisant un seul? Comment pouvons nous aider? Publiez le code présentant le problème, publiez ce que vous obtenez et publiez ce que vous attendiez, au lieu d'une description du problème. –
Pouvez-vous poster la définition complète de votre classe? – PaulJWilliams
Pas lié à votre question, mais, au lieu de (* this) .size utilisez cette-> taille –