 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
DELORME Frederic Guest
|
Posted: Mon Sep 27, 2004 10:16 am Post subject: Problem with a static member in a CNode class ... |
|
|
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> ------------------------------------------------------------
| Quote: | */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 ? )
thanks for any help !
F.Delorme.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Mon Sep 27, 2004 9:07 pm Post subject: Re: Problem with a static member in a CNode class ... |
|
|
DELORME Frederic wrote:
| Quote: | I've the class CNode where there is a static member m_BaseNode:
[...]
class CNode{
static CNode *m_BaseNode;
};
|
[snipped lots of other code]
| Quote: | The problem is that i've the next error while compiling:
*/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'
|
Those are not compiler errors but linker errors. The problem is that you
declared the static element, but you never defined it. Add this line
CNode* CNode::m_BaseNode;
somewhere in your .cpp file.
BTW: please try reducing your problem to the smallest possible piece of code
that still shows the problem next time.
Uli
--
FAQ: http://parashift.com/c++-faq-lite/
/* bittersweet C++ */
default: break;
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Mon Sep 27, 2004 9:22 pm Post subject: Re: Problem with a static member in a CNode class ... |
|
|
DELORME Frederic wrote:
| Quote: | I've the class CNode where there is a static member m_BaseNode:
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'
------------------------------------------------------------
snip |
You only declared CNode::m_BaseNode. Static members have to be
defined too (in exactly one translation unit). In this case you
should put "CNode * CNode::m_BaseNode;" in the implementation file.
--
Ben Hutchings
Absolutum obsoletum. (If it works, it's out of date.) - Stafford Beer
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|