2009-05-07 7 views
2

私はリンクリストクラスを持っていて、何らかの理由で1つのintをインクリメントしたとき オブジェクト何かの理由でlinked1.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());} 
}; 
+3

どの問題がありますか? 1つだけを使ってコードを表示することで、2つのリストの問題を実証していますか?どのように我々は助けることができます?オーバービューの問題の説明ではなく、問題を示すコードを投稿し、取得した内容を投稿し、期待した内容を投稿します。 –

+0

あなたはあなたのクラスの完全な定義を投稿できますか? – PaulJWilliams

+1

質問には関係ありませんが、(* this).sizeの代わりにthis-> sizeを使用してください。 –

答えて

1

をより多くのコードを見ることなく、私は確認することはできませんが、それはv1とv2可能であるが、実際には同じリンクリストオブジェクトへの参照ですか?

+0

サイズが増分されていることを除いて、v2は空のままなので、同じ構造ではないようです。 – Meir

2

linked2は同じ構造をどういう意味ですか? (今、完全なコードがあることを目に見える(と与えられたものadd_quiet(...)とを追加します(?& linked1.data) あなたは

編集を

printf("adress1 %p", &linked1) 
printf("adress2 %p", &linked2) 
printf("adress1/size %p", &linked1.size) 
printf("adress2/size %p", &linked2.size) 
repectively Vlistの他のメンバーのために

を試すことができます...)原則として同じことをする)私は "共有"サイズのクラスフィールドは問題ではないと思う。デバッガを使用し、リストのアドレスを追跡します。これはむしろ奇妙ですが、私は解決策にこれまで以上に興味があります

0

何らかの理由で宣言される前であっても、デバッガがv2の値を教えていたようです。 (私はコードブロックを使用しています)。
最後に、これらの変数をページの先頭に宣言しましたが、この特定の問題は修正されました。
プロジェクト全体をこのWebサイトにアップロードせずに説明するのが難しいいくつかの他の問題を説明する設計上の欠陥を発見したいと考えていました。そのような運はありません。
私のプログラムの残りの部分については... sigh

とにかく、ありがとうございました。

+0

Code :: Blocksの基礎となるコンパイラは何ですか?あなたの選択はg ++だと思いますか? – msi