Domanda

Sto cercando di scrivere un'implementazione di un albero 2-3-4 in c ++. Sono passato un po 'di tempo da quando ho usato i modelli e sto riscontrando degli errori. Ecco il mio framework di codice estremamente semplice:
    node.h:

    #ifndef TTFNODE_H  
    #define TTFNODE_H  
    template <class T>  
    class TreeNode  
    {  
      private:  
      TreeNode();  
      TreeNode(T item);  
      T data[3];  
      TreeNode<T>* child[4];  
      friend class TwoThreeFourTree<T>;   
      int nodeType;  
    };  
    #endif

node.cpp:

#include "node.h"

using namespace std;
template <class T>
//default constructor
TreeNode<T>::TreeNode(){
}

template <class T>
//paramerter receving constructor
TreeNode<T>::TreeNode(T item){
data[0] = item;
nodeType = 2;
}

TwoThreeFourTree.h

#include "node.h"
#ifndef TWO_H
#define TWO_H
enum result {same, leaf,lchild,lmchild,rmchild, rchild};
template <class T> class TwoThreeFourTree
{
  public:
    TwoThreeFourTree();

  private:
    TreeNode<T> * root;
};
#endif

TwoThreeFourTree.cpp:

#include "TwoThreeFourTree.h"
#include <iostream>
#include <string>

using namespace std;

template <class T>
TwoThreeFourTree<T>::TwoThreeFourTree(){
  root = NULL;
}

E main.cpp:

#include "TwoThreeFourTree.h"
#include <string>
#include <iostream>
#include <fstream>

using namespace std;

int main(){
  ifstream inFile;
  string filename = "numbers.txt";
  inFile.open (filename.c_str());
  int curInt = 0;
  TwoThreeFourTree <TreeNode> Tree;

  while(!inFile.eof()){
    inFile >> curInt;
    cout << curInt << " " << endl;
  }

  inFile.close();
}

E quando provo a compilare dalla riga di comando con:      g ++ main.cpp node.cpp TwoThreeFourTree.cpp

Ottengo i seguenti errori:

In file included from TwoThreeFourTree.h:1,  
             from main.cpp:1:  
node.h:12: error: ‘TwoThreeFourTree’ is not a template  
main.cpp: In function ‘int main()’:  
main.cpp:13: error: type/value mismatch at argument 1 in template parameter list for ‘template<class T> class TwoThreeFourTree’  
main.cpp:13: error:   expected a type, got ‘TreeNode’  
main.cpp:13: error: invalid type in declaration before ‘;’ token  
In file included from node.cpp:1:  
node.h:12: error: ‘TwoThreeFourTree’ is not a template  
In file included from TwoThreeFourTree.h:1,  
             from TwoThreeFourTree.cpp:1:  
node.h:12: error: ‘TwoThreeFourTree’ is not a template  

La mia domanda principale è perché sta dicendo l'errore "quot:" TwoThreeFourTree "non è un modello " ;. Qualcuno ha qualche idea? Grazie per tutti i consigli / aiuto in anticipo ... Dan

È stato utile?

Soluzione

Devi solo dichiararlo come modello quando usi la parola chiave friend. Stai usando una sintassi errata per una dichiarazione di amicizia nel tuo codice. Quello che vuoi scrivere è:

template <class U> friend class TwoThreeFourTree;

Altri suggerimenti

La soluzione che è stata accettata ha il leggero problema di aprire la tua classe a qualsiasi istanza del modello TwoThreeFourTree , non solo a quelli che condividono lo stesso tipo di istanza.

Se vuoi solo aprire la classe a istanze dello stesso tipo puoi usare la sintassi seguente:

template <typename T> class TwoThreeFourTree; // forward declare the other template
template <typename T>
class TreeNode {
   friend class TwoThreeFourTree<T>;
   // ...
};
template <typename T>
class TwoThreeFourTree {
   // ...
};
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top