-Ivan-
07-08-2013, 18:34
Sto facendo danni cercando di muovere qualche passo avanti in C++.
Devo creare un albero binario (un decision tree) e navigare i nodi controllando delle condizioni su di essi per sapere quale ramo scelgliere per continuare la navigazione.
Il problema è che ho un errore Unresolved external symbol che non capisco.
Sembra che non veda la variabile instance dichiarata come static nella classe DecisionTree.
Il problema si verifica in questo punto del codice. Questo è un metodo della classe nodo (FloatDecision è un tipo di nodo particolare dell'albero):
DecisionTreeNode& FloatDecision::GetBranch()const
{
//get an instance of the singleton class Decision Tree
DecisionTree *tree = DecisionTree::GetDecisionTree();
if( testValue >= minValue && testValue <= maxValue )
{
return *( tree->GetNode(trueNode) );
}
return *( tree->GetNode(falseNode) );
}
L'errore è:
Error 1
error LNK2001: unresolved external symbol "private: static class DecisionTree * DecisionTree::instance"
(?instance@DecisionTree@@0PAV1@A)
C:\Users\Ivan\Documents\C++ development\DecisionTree\DecisionTree\FloatDecision.obj DecisionTree
Questa è la classe singleton dove la variabile instance è dichiarata ed ha un metodo per ricavarla che si chiama GetDecisionTree():
#ifndef DECISION_TREE
#define DECISION_TREE
//------------------------------------------------------------------------
//
// Name: DecisionTree.h
//
// Desc: This is the SINGLETON class defining the decision tree.
//
// Author: Ivan Fesani
//
// Date: August 2013
//
//----------------------------------------------------------------------
#include <cstdlib>
#include <vector>
#include "DecisionTreeNode.h"
class DecisionTree
{
private:
//private constructor for the singleton class
DecisionTree(){}
static DecisionTree* instance;
//all the nodes are stored in a vector to have a compact block of memory storing it to avoid cache misses
std::vector<DecisionTreeNode *> nodes;
public:
~DecisionTree(){}
//get an instance of this class
static DecisionTree* GetDecisionTree(){ if( instance == NULL ) instance = new DecisionTree; return instance; }
//get the node at some index
DecisionTreeNode* GetNode(int p_idx);
//add a node to the tree
void AddNode(DecisionTreeNode& node, std::string p_info);
//print the tree
void Print();
};
#endif
Devo creare un albero binario (un decision tree) e navigare i nodi controllando delle condizioni su di essi per sapere quale ramo scelgliere per continuare la navigazione.
Il problema è che ho un errore Unresolved external symbol che non capisco.
Sembra che non veda la variabile instance dichiarata come static nella classe DecisionTree.
Il problema si verifica in questo punto del codice. Questo è un metodo della classe nodo (FloatDecision è un tipo di nodo particolare dell'albero):
DecisionTreeNode& FloatDecision::GetBranch()const
{
//get an instance of the singleton class Decision Tree
DecisionTree *tree = DecisionTree::GetDecisionTree();
if( testValue >= minValue && testValue <= maxValue )
{
return *( tree->GetNode(trueNode) );
}
return *( tree->GetNode(falseNode) );
}
L'errore è:
Error 1
error LNK2001: unresolved external symbol "private: static class DecisionTree * DecisionTree::instance"
(?instance@DecisionTree@@0PAV1@A)
C:\Users\Ivan\Documents\C++ development\DecisionTree\DecisionTree\FloatDecision.obj DecisionTree
Questa è la classe singleton dove la variabile instance è dichiarata ed ha un metodo per ricavarla che si chiama GetDecisionTree():
#ifndef DECISION_TREE
#define DECISION_TREE
//------------------------------------------------------------------------
//
// Name: DecisionTree.h
//
// Desc: This is the SINGLETON class defining the decision tree.
//
// Author: Ivan Fesani
//
// Date: August 2013
//
//----------------------------------------------------------------------
#include <cstdlib>
#include <vector>
#include "DecisionTreeNode.h"
class DecisionTree
{
private:
//private constructor for the singleton class
DecisionTree(){}
static DecisionTree* instance;
//all the nodes are stored in a vector to have a compact block of memory storing it to avoid cache misses
std::vector<DecisionTreeNode *> nodes;
public:
~DecisionTree(){}
//get an instance of this class
static DecisionTree* GetDecisionTree(){ if( instance == NULL ) instance = new DecisionTree; return instance; }
//get the node at some index
DecisionTreeNode* GetNode(int p_idx);
//add a node to the tree
void AddNode(DecisionTreeNode& node, std::string p_info);
//print the tree
void Print();
};
#endif