【问题标题】:c++ templated friend classc++模板朋友类
【发布时间】:2009-11-27 20:39:50
【问题描述】:

我正在尝试用 C++ 编写 2-3-4 树的实现。我已经有一段时间没有使用模板了,我遇到了一些错误。这是我非常基本的代码框架:
节点.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;
}

还有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();
}

当我尝试从命令行编译时: g++ main.cpp node.cpp TwoThreeFourTree.cpp

我收到以下错误:

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  

我的主要问题是为什么它说“错误:‘TwoThreeFourTree’不是模板”。有没有人有任何想法?感谢您提前提供所有建议/帮助... 丹

【问题讨论】:

  • 不完全是“完全”的骗局,是吗?
  • @Pavel:不是完全重复的,这里用户不希望模板的所有实例都可以访问,而只希望具有相同特定类型的实例。 TwoThreeFourTree&lt;int&gt; 应该可以访问TreeNode&lt;int&gt;,但不一定可以访问TreeNode&lt;double&gt;
  • 貌似除了TwoThreeFourTree没有别的可以用TreeNode,还是应该看看;为什么不直接将节点类型设为树类的private 嵌套非模板类?

标签: c++ class templates g++ friend


【解决方案1】:

已被接受的解决方案存在一个小问题,即打开您的类以访问TwoThreeFourTree 模板的任何实例化,而不仅仅是那些共享相同实例化类型的实例。

如果您只想将类打开到相同类型的实例化,您可以使用以下语法:

template <typename T> class TwoThreeFourTree; // forward declare the other template
template <typename T>
class TreeNode {
   friend class TwoThreeFourTree<T>;
   // ...
};
template <typename T>
class TwoThreeFourTree {
   // ...
};

【讨论】:

  • 谢谢,没有意识到我必须做一个前向声明才能让它工作!谢谢!!! :)
【解决方案2】:

您只需要在使用friend关键字时将其声明为模板。您在代码中对朋友声明使用了不正确的语法。你要写的是:

template <class U> friend class TwoThreeFourTree;

【讨论】:

  • 感谢 Charles Salvia,效果很好!现在要弄清楚,“main.cpp:13: 错误: 'template class TwoThreeFourTree' main.cpp:13 模板参数列表中参数 1 的类型/值不匹配: 错误: 期望一个类型, 得到了 'TreeNode '...
  • 原始代码中的意图和修改后代码中的语义略有不同:这里您将TwoThreeFourTree(具有任何类型U)的所有实例声明为朋友,因此@ 987654323@ 可以访问 `TreeNode'
猜你喜欢
  • 2012-01-21
  • 2014-10-03
  • 1970-01-01
  • 1970-01-01
  • 2016-10-29
  • 2011-10-14
  • 2018-07-15
  • 2017-02-09
  • 2011-03-18
相关资源
最近更新 更多