Problem with a static member in a class ?

Discussion in 'C++' started by DELORME Frederic, Sep 26, 2004.

  1. I've the class CNode where there is a static Member m_BaseNode:

    The declaration file is : cnode.h

    <code> -----------------------------------------------------------
    #include <iostream>
    #include <cstring>

    #ifndef E3DCNODE_H
    #define E3DCNODE_H

    using namespace std;

    namespace E3D {

    /**
    Noeud d'un arbre

    @author DELORME Frederic
    */
    class CNode{
    public:
            CNode();
            CNode(string psNodeName);
            CNode(string psNodeName, CNode *poParentNode);

            ~CNode();
            void init();
            void addChild(CNode *poNodeChild);
            void attachTo(CNode *poNodeParent);
            void remove(CNode *poNodeChild);

    protected:
            static CNode *m_BaseNode;
            // Nom du noeud
            string m_Name;
            // Noeud Racine
            // Parent de ce noeud
            CNode *m_Parent;
            // Premier Enfant de ce noeud
            CNode *m_Child;
            // Enfant suivant de ce noeud
            CNode *m_Next;
            // Enfant précédent de ce noeud
            CNode *m_Prev;
    };

    };
    #endif
    ----------------------------------------------------------- <code>

    And the implemantation file is: cnode.cpp

    <code> -----------------------------------------------------------
    #include "cnode.h"

    namespace E3D {

    CNode::CNode(){
            this->m_Name = "noname";
            this->init();
    }
    /*!
            Création d'un noeud attaché à un parent.
            \fn E3D::CNode::CNode(string *psNodeName)
            \param psNodeName : Nom du noeud à créer.
     */
    CNode::CNode(string psNodeName)
    {
            this->m_Name = psNodeName;
            this->init();
    }
    /*!
            Création d'un noeud attaché à un parent.
            \fn E3D::CNode::CNode(CNode *poParentNode)
            \param poNodeName : Nom du noeud à créer.
            \param poParentNode : Noeud parent.
     */
    CNode::CNode(string psNodeName, CNode *poParentNode)
    {
            this->m_Name = psNodeName;
            poParentNode->addChild(this);
    }
    /*!
            Constructeur par défaut
            \fn E3D::CNode::CNode().
    */
    CNode::~CNode()
    {
            if(this->m_Child) delete this->m_Child;
            if(this->m_Next) delete this->m_Next;
            if(this->m_Prev) delete this->m_Prev;
    }

    void CNode::init(){
            if(!this->m_BaseNode){
                    // oui, alors, on l'initialise
                    this->m_BaseNode = this;
                    this->m_Parent = NULL;
            }else{
                    // Sinon on l'ajoute en tant qu'enfant.
                    this->m_BaseNode->addChild(this);
            }
    }

    /*!
            Ajout d'un enfant à cet objet.
            \fn E3D::CNode::addChild(const CNode *poNodeChild).
            \param poNodeChild : Noeud enfant à ajouter.
    */
    void CNode::addChild(CNode *poNodeChild){

            poNodeChild->attachTo(this);
    /*      // Ce noeud a-t-il des enfants ?
            if( this->m_Child == NULL ){
                    // NON, alors on l'ajoute en tant que premier enfant
                    this->m_Child = poNodeChild;
                    poNodeChild->m_Parent = this;
            }else{
                    // OUI, alors, on recherche le dernier enfant
                    CNode *ptrNode = this->m_Child;
                    while( ptrNode->m_Next != NULL ){
                            ptrNode = ptrNode->m_Next;
                    }
                    ptrNode->m_Next = poNodeChild;
                    poNodeChild->m_Prev = ptrNode;
            }
    */
    }
    /*!
            Attacher le Noeud courant au noeud parent donné.
            \fn E3D::CNode::attachTo(CNode *poNodeParent)
            \param poNodeParent : Parent auquel attacher le noeud.
    */
    void CNode::attachTo(CNode *poNodeParent){
            if( poNodeParent->m_Child == NULL ){
                    poNodeParent->m_Child = this;
            }else{
                    CNode *ptrNode = poNodeParent->m_Child;
                    while( ptrNode->m_Next != NULL ){
                            ptrNode = ptrNode->m_Next;
                    }
                    ptrNode->m_Next = this;
            }
    }

    /*!
            Supprimer le noeud donné.
            \fn E3D::CNode::remove(CNode *poNode).
            \param poNode : Noeud à supprimer
    */
    void CNode::remove(CNode *poNode){
            // on récupère les précédents et suivants
            if(poNode){
                    CNode *ptrNext = poNode->m_Next;
                    CNode *ptrPrev = poNode->m_Prev;
                    if( &poNode != &this->m_BaseNode ){

                    // On enlève le noeud de la chaine
                    if(ptrPrev!=NULL && ptrPrev->m_Next!=NULL) ptrPrev->m_Next = ptrNext;
                    if(ptrNext!=NULL && ptrNext->m_Prev!=NULL) ptrNext->m_Prev = ptrPrev;
                    delete poNode;
                    }else{
                            delete this;
                    }
            }
    }

    };
    ----------------------------------------------------------- <code>

    The problem is that i've the next error while compiling:

    <log> ------------------------------------------------------------
    */home/fred/Projets/cpp/testsdl/src/cnode.cpp:61: undefined reference to
    `E3D::CNode::m_BaseNode'
    *cnode.o(.text+0x50b):/home/fred/Projets/cpp/testsdl/src/cnode.cpp:63:
    undefined reference to `E3D::CNode::m_BaseNode'
    *cnode.o(.text+0x523):/home/fred/Projets/cpp/testsdl/src/cnode.cpp:67:
    undefined reference to `E3D::CNode::m_BaseNode'
    */home/fred/Projets/cpp/testsdl/src/cnode.cpp:122: undefined reference to
    `E3D::CNode::m_BaseNode'
    ------------------------------------------------------------ <log>

    anybody Any idea ? :eek:)

    thanks for any help !

    F.Delorme.
     
    DELORME Frederic, Sep 26, 2004
    #1
    1. Advertising

  2. DELORME Frederic

    David Hilsee Guest

    "DELORME Frederic" <> wrote in message
    news:41571b07$0$25680$...
    > I've the class CNode where there is a static Member m_BaseNode:
    >
    > The declaration file is : cnode.h
    >
    > <code> -----------------------------------------------------------
    > #include <iostream>
    > #include <cstring>
    >
    > #ifndef E3DCNODE_H
    > #define E3DCNODE_H
    >
    > using namespace std;
    >
    > namespace E3D {
    >
    > /**
    > Noeud d'un arbre
    >
    > @author DELORME Frederic
    > */
    > class CNode{
    > public:
    > CNode();
    > CNode(string psNodeName);
    > CNode(string psNodeName, CNode *poParentNode);
    >
    > ~CNode();
    > void init();
    > void addChild(CNode *poNodeChild);
    > void attachTo(CNode *poNodeParent);
    > void remove(CNode *poNodeChild);
    >
    > protected:
    > static CNode *m_BaseNode;
    > // Nom du noeud
    > string m_Name;
    > // Noeud Racine
    > // Parent de ce noeud
    > CNode *m_Parent;
    > // Premier Enfant de ce noeud
    > CNode *m_Child;
    > // Enfant suivant de ce noeud
    > CNode *m_Next;
    > // Enfant précédent de ce noeud
    > CNode *m_Prev;
    > };

    <snip>

    > The problem is that i've the next error while compiling:
    >
    > <log> ------------------------------------------------------------
    > */home/fred/Projets/cpp/testsdl/src/cnode.cpp:61: undefined reference to
    > `E3D::CNode::m_BaseNode'
    > *cnode.o(.text+0x50b):/home/fred/Projets/cpp/testsdl/src/cnode.cpp:63:
    > undefined reference to `E3D::CNode::m_BaseNode'
    > *cnode.o(.text+0x523):/home/fred/Projets/cpp/testsdl/src/cnode.cpp:67:
    > undefined reference to `E3D::CNode::m_BaseNode'
    > */home/fred/Projets/cpp/testsdl/src/cnode.cpp:122: undefined reference to
    > `E3D::CNode::m_BaseNode'
    > ------------------------------------------------------------ <log>
    >
    > anybody Any idea ? :eek:)
    >
    > thanks for any help !


    Where's the definition of E3D::CNode::m_BaseNode? See the FAQ
    (http://www.parashift.com/c -faq-lite/) Section 10 (Constructors) question
    10 (Why are classes with static data members getting linker errors?).

    --
    David Hilsee
     
    David Hilsee, Sep 26, 2004
    #2
    1. Advertising

  3. David Hilsee wrote:

    >
    > Where's the definition of E3D::CNode::m_BaseNode? See the FAQ
    > (http://www.parashift.com/c -faq-lite/) Section 10 (Constructors)
    > question 10 (Why are classes with static data members getting linker
    > errors?).
    >


    thanks a lot, i'll run there !

    --
    <hr width="100%" size="2" noshadow/>
    F.Delorme<>
     
    DELORME Frederic, Sep 26, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,941
    Thomas Weidenfeller
    Oct 12, 2005
  2. Sam
    Replies:
    4
    Views:
    491
    The Directive
    Jan 13, 2004
  3. Siemel Naran
    Replies:
    4
    Views:
    838
    Micah Cowan
    Jan 12, 2005
  4. .rhavin grobert
    Replies:
    2
    Views:
    327
    Victor Bazarov
    Aug 21, 2007
  5. dolphin
    Replies:
    3
    Views:
    1,399
    Pete Becker
    Dec 5, 2007
Loading...

Share This Page