Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 15
  1. #1
    Utente di HTML.it
    Registrato dal
    Jul 2011
    Messaggi
    106

    [c++]Problema compilazione

    Ragazzi scusate,
    dovrei provare degli esercizi,tramite del codice fatto dal mio prof.Il problema é che non riesco a compilare mi da sempre
    " [Linker error] undefined reference to `IntGenTree::esercizio(BaseGenTree<int>::Node*)' ".Non riesco a trovare una soluzione..
    Qualcuno può aiutarmi??
    Vi posto il codice..
    IntGenTree.h
    codice:
        #ifndef INTGTREE_H
        #define INTGTREE_H
    
        #include "BaseGenTree.h"
    
        class IntGenTree: public BaseGenTree<int>{
        protected:
           /* inserire qui le intestazioni */
           static void esercizio(Node* t);
        public:
           /* inserire qui le intestazioni pubbliche */
          void _esercizio(){esercizio(root);}
         
        };
    
        #endif
    IntGenTree.cpp
    codice:
        #include "IntGenTree.h"
        #include "BaseGenTree.h"
    
        /* Inserire qui il codice */
    
        void IntGenTree::esercizio(Node* t, int& max){
           if (t == NULL) { max = 0; return 0; }
           // maxC = massimo tra i discendenti
           // maxS = massimo tra i prossimi fratelli
           int maxC, maxS;
           int count;
           count = countDescendantsRulers(t->firstChild, maxC);
           count += countDescendantsRulers(t->nextSibling, maxS);
           // max = max(t->info, maxC, maxS) = massimo globale
           if (t->info > maxC){
              // il nodo domina i suoi discendenti:
              count++;
              max = (t->info > maxS) ? t->info : maxS;
           }
           else
              max = (maxC > maxS) ? maxC : maxS;
           return count;  }
    BaseGenTree.h
    codice:
        #ifndef BASEGTREE_H
        #define BASEGTREE_H
    
        #include <iostream>
        #include <iomanip>
    
        // Necessarie per le dichiarazioni 'friend'.
        template <class T> class BaseGenTree;
        template <class T> std::ostream& operator<<(std::ostream&, const BaseGenTree<T>&);
        template <class T> std::istream& operator>>(std::istream&, BaseGenTree<T>&);
    
        /**
        * Classe modello di un albero binario semplice.
        */
        template <class T>
        class BaseGenTree{
        protected:
    
          /**
           * Un nodo dell'albero.
           */
          struct Node{
           
            T info;      /**< Il contenuto del nodo */
            Node* firstChild;   /**< Il puntatore al primo figlio */
            Node* nextSibling;   /**< Il puntatore al prossimo fratello */
           
            /**
             * Costruisce un nodo di valore x.
             */
            Node(const T& x) : info(x), firstChild(NULL), nextSibling(NULL){};
          };            
         
          Node* root;         /**< Il puntatore alla radice dell'albero */
         
          static void deleteTree(Node*&);
          static std::ostream& putTo(const Node*, std::ostream&);
          static std::istream& getFrom(Node*&, std::istream&);
        public:
          /**
           * Costruisce un albero binario vuoto.
           */
          BaseGenTree() : root(NULL){};
          /**
           * Distrugge l'albero binario.
           */
          virtual ~BaseGenTree() {deleteTree(root);};
    
          friend std::ostream& operator<< <>(std::ostream&, const BaseGenTree<T>&);
          friend std::istream& operator>> <>(std::istream&, BaseGenTree<T>&); 
        };
    
    
    
        /* =============== IMPLEMENTAZIONE =============== */
    
        /**
        * Cancella tutti i nodi dell'albero t.
        */
        template <class T>
        void BaseGenTree<T>::deleteTree(Node*& t) {
          if (t != NULL) {
            deleteTree(t->firstChild);
            deleteTree(t->nextSibling);
            delete t;
            t = NULL;
          }
        }
    
        /**
        * Invia un albero su stream di output nel formato: [1[3][4[][5]]].
        */
        template <class T>
        std::ostream& BaseGenTree<T>::putTo(const Node* t, std::ostream& os){
           os << '[';
           if(t != NULL)
           {
              // stampo la radice:
              os << t->info;
              // stampo i sottoalberi:
              for(Node* i = t->firstChild; i != NULL; i = i->nextSibling)
                 putTo(i, os);
           }     
           os << ']';
           return os;
        }
    
        /**
        * Ridefinizione dell'operatore 'put to' per la classe BaseGenTree.
        * Invia un albero su stream di output nel formato: [1[3][4[][5]]].
        */
        template<class T>
        std::ostream& operator<<(std::ostream& os, const BaseGenTree<T>& t){
           return BaseGenTree<T>::putTo(t.root, os);
        }
    
        /**
        * Acquisisce un albero da stream di input nel formato: [1[3][4[][5]]].
        */
        template <class T>
        std::istream& BaseGenTree<T>::getFrom(Node*& t, std::istream& is){
           deleteTree(t);
    
           char cbuf;
           is >> cbuf;
           if (cbuf != '['){
              is.putback(cbuf);
              is.clear(std::ios_base::failbit);
              return is;
           }
           is >> cbuf;
           if (cbuf == ']') // albero vuoto: []
              return is;
           is.putback(cbuf);
           T info;
           
           is >> info >> cbuf;
           if (is.fail()) return is;
           t = new Node(info);
           Node** subTree = &(t->firstChild);
           while (cbuf == '['){
              // acquisisco un sottoalbero:
              is.putback(cbuf);
              getFrom(*subTree, is);
              is >> cbuf;
              subTree = &((*subTree)->nextSibling);
              if (cbuf == ']'){
                 return is;
              }
           }
           return is;
        }
    
        /**
        * Ridefinizione dell'operatore 'get from' per la classe BaseGenTree.
        * Acquisisce un albero da stream di input nel formato: [1[3][4[][5]]].
        */
        template <class T>
        std::istream& operator>>(std::istream& is, BaseGenTree<T>& t){
           return BaseGenTree<T>::getFrom(t.root, is);
        }
    
        #endif
    main.cpp
    codice:
        #include "IntGenTree.h"
        using namespace std;
    
        int main(){
            IntGenTree t;
            cout<<t<<endl;
            t._esercizio();
            system("PAUSE");
            return 0;
            }
    Grazie..

  2. #2
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,481
    Quale ambiente/compilatore hai usato?

    Che tipo di progetto?

    Quali file hai inserito nel progetto?
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  3. #3
    Utente di HTML.it
    Registrato dal
    Jul 2011
    Messaggi
    106
    Ho quello che usiamo all'uni,il Dev c++
    Ho creato un nuovo progetto con il main e poi ho aperto all'interno anche gli altri file .h,.cpp
    e ho compitalo,quando ho avviato il costruttore della classe,mi ha dato.Ma quando provo l'esercizio mi da errore...
    Ho inserito tutti i file che ho postato..
    grazie

  4. #4
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,481
    Nella classe IntGenTree non esiste un metodo con la firma

    esercizio(Node* t, int& max)

    E inoltre questo metodo è dichiarato void ma restituisce un valore con

    return count;
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  5. #5
    Utente di HTML.it
    Registrato dal
    Jul 2011
    Messaggi
    106
    Rigurado al ritorno della funzione hai perfettamente ragione,non mi ero accorto da che ho provato impostazioni diverese,ma questo purtroppo npon risolve il problema.
    Il prima caso é sicuramente il problema...
    Sapresti dirmi cosa modificare in modo più esplicito,da che dato che IntBeenTree é una Classe figlia della classe BaseBeenTree,non ho molta dimestichezza con ereditarieta di classe ecc..
    Ho provato ma non mi va,tra l'altro ho provato altre funzioni oltre a quella citata,ma ho sempre lo stesso problema(anche con esercizio(Elem* l),senza altri valori..
    Grazie

  6. #6
    se nel .H il metodo si chiama "_esercizio(...)" e nel .C tu fai riferimento ad "esercizio(...)" senza il trattino basso iniziale ti darà sempre problemi in compilazione.

    Correggi il nome in uno dei due files.
    Administrator of NAMDesign.Net

  7. #7
    Utente di HTML.it
    Registrato dal
    Jul 2011
    Messaggi
    106
    Guarda non vorrei sbagliarmi,ma nel file c'è anche un _esercizio(),con all'interno una chiamata a esercizio(Elem*)...

  8. #8
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,481
    Ti ripeto .. il problema è che nella classe c'è il metodo

    esercizio(Node* t)

    ma nell'implementazione tu scrivi

    esercizio(Node* t, int& max)

    Adesso sta a te capire perché quel parametro

    int& max

    in più.
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  9. #9
    Utente di HTML.it
    Registrato dal
    Jul 2011
    Messaggi
    106
    Ragazzi ho fatto in questo modo,ma niente
    codice:
    #ifndef INTGTREE_H
    #define INTGTREE_H
    
    #include "BaseGenTree.h"
    
    class IntGenTree: public BaseGenTree<int>{
    protected:
       /* inserire qui le intestazioni */
       static void esercizio(Node*,int);
    public:
       /* inserire qui le intestazioni pubbliche */
      void _esercizio();
      
    };
    
    #endif
    codice:
    /* Inserire qui il codice */
    
    void IntGenTree::esercizio(Node* t,int max){
       if (t == NULL) { max = 0; return 0; }
       // maxC = massimo tra i discendenti
       // maxS = massimo tra i prossimi fratelli
       int maxC, maxS;
       int count;
       count = countDescendantsRulers(t->firstChild, maxC);
       count += countDescendantsRulers(t->nextSibling, maxS);
       // max = max(t->info, maxC, maxS) = massimo globale
       if (t->info > maxC){
          // il nodo domina i suoi discendenti:
          count++;
          max = (t->info > maxS) ? t->info : maxS;
       }
       else
          max = (maxC > maxS) ? maxC : maxS;
       return count;  }
    
    
    void IntGenTree::_esercizio(){ int n=5;
                                   esercizio(root,n);
                                        }

  10. #10
    Utente di HTML.it
    Registrato dal
    Jul 2011
    Messaggi
    106
    Provato anche a cancellarei file di compilazione e a creare un nuovo progetto..
    niente.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.