雪雪诺儿

导航

使用模板类,创建树。使用头文件分离模板类的申明与实现时出错

利用模板类,创建树。

模板类:使用头文件分离申明与实现,理论上可行,但实际不可行。故因放在同一个.cpp中。

C++实现:

 

 1 #include<stdio.h>
 2 #include<iostream>
 3 using namespace std;
 4 template<class elemType>
 5     struct nodeType{
 6         elemType data;
 7         nodeType * lchild;
 8         nodeType * rchild;
 9     };
10 template<class elemType>
11     class binaryTree
12     {
13     public:
14         void creat();    
15     protected:
16         nodeType<elemType> *root;
17     };
18 
19 int main(){
20     binaryTree<char> T;
21     T.creat();
22     getchar();
23 }
24 
25 template<class elemType>
26 void binaryTree<elemType>::creat(){
27     root=(nodeType<elemType>*)malloc(sizeof(nodeType<elemType>));
28     root->data='A';
29     root->lchild=NULL;
30     root->lchild=NULL;
31     cout<<root->data;
32     free(root);
33 }

 

posted on 2016-04-12 19:08  雪雪诺儿  阅读(210)  评论(0)    收藏  举报