Problem with a static member in a class ?

D

DELORME Frederic

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.
 
D

David Hilsee

DELORME Frederic said:
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;
};
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?).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top