|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Mar 2003
Città: Rimini
Messaggi: 1843
|
[C++] Unresolved external simbol su classe singleton
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): Codice:
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) ); } Codice:
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 Codice:
#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 Ultima modifica di -Ivan- : 07-08-2013 alle 18:41. |
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: May 2001
Messaggi: 12849
|
Devi creare un file cpp in cui dichiarare tutte le variabili statiche.
In ogni caso ti suggerisco di usare questa forma alternativa per il singleton in C++: Codice:
static DecisionTree& GetDecisionTree() { static DecisionTree instance; return instance; } PS: non scrivere il codice tutto su una linea ![]() |
![]() |
![]() |
![]() |
#3 |
Senior Member
Iscritto dal: Jan 2012
Messaggi: 1267
|
Come dice WarDuck, le variabili statiche vanno istanziate in un file sorgente, e lì vivranno per la durata del programma. Ce n'è una sola per tutti gli oggetti della classe in cui sono dichiarate, non ne viene creata una per ogni oggetto, quindi hanno bisogno di stare fisicamente separate.
|
![]() |
![]() |
![]() |
#4 |
Senior Member
Iscritto dal: Mar 2003
Città: Rimini
Messaggi: 1843
|
Grazie per le risposte. Ho compreso e memorizzato (speriamo)
![]() |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 11:15.