![]() |
#2
玩出来的代码2010-10-31 15:05
|
原来是这样传递的,没有任何问题。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
template <class T>
class BinaryTreeNode
{
template <class type>
friend class BinaryTree;
public:
BinaryTreeNode()
{
LeftChild = RightChild = NULL;
}
BinaryTreeNode(const T& e)
{
data = e;
LeftChild = RightChild = NULL;
}
BinaryTreeNode(const T& e,BinaryTreeNode *l,BinaryTreeNode* r)
{
data = e;
LeftChild = l;
RightChild = r;
}
private:
T data;
BinaryTreeNode<T>* LeftChild,
*RightChild;
};
template<class T>
class BinaryTree
{
public :
BinaryTree()
{
root = NULL;
}
void PreOrder(void (*Visit)(BinaryTreeNode<T>* u))
{
PreOrder(Visit,root);
}
private:
BinaryTreeNode<T> * root;
int nodeCount;
void PreOrder(void(*Visit)(BinaryTreeNode<T> * u),BinaryTreeNode<T> * t)
{
if(t)
{
Visit(t);
PreOrder(Visit,t->LeftChild);
PreOrder(Visit,t->RightChild);
}
}
};
int nodeCount = 0;
template<class T>
void ct(BinaryTreeNode<T>* u)
{
nodeCount++;
}
int main()
{
BinaryTree<int> tree;
tree.PreOrder(ct);
return 0;
}
但是我想把ct函数写在BinaryTree的内部,按照我的写法却无法编译#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
template <class T>
class BinaryTreeNode
{
template <class type>
friend class BinaryTree;
public:
BinaryTreeNode()
{
LeftChild = RightChild = NULL;
}
BinaryTreeNode(const T& e)
{
data = e;
LeftChild = RightChild = NULL;
}
BinaryTreeNode(const T& e,BinaryTreeNode *l,BinaryTreeNode* r)
{
data = e;
LeftChild = l;
RightChild = r;
}
private:
T data;
BinaryTreeNode<T>* LeftChild,
*RightChild;
};
template<class T>
class BinaryTree
{
public :
BinaryTree()
{
root = NULL;
}
void PreOrder(void (*Visit)(BinaryTreeNode<T>* u))
{
PreOrder(Visit,root);
}
private:
BinaryTreeNode<T> * root;
int nodeCount;
void PreOrder(void(*Visit)(BinaryTreeNode<T> * u),BinaryTreeNode<T> * t)
{
if(t)
{
Visit(t);
PreOrder(Visit,t->LeftChild);
PreOrder(Visit,t->RightChild);
}
}
};
int nodeCount = 0;
template<class T>
void ct(BinaryTreeNode<T>* u)
{
nodeCount++;
}
int main()
{
BinaryTree<int> tree;
tree.PreOrder(ct);
return 0;
}
我是这样写的

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
template <class T>
class BinaryTreeNode
{
template <class type>
friend class BinaryTree;
public:
BinaryTreeNode()
{
LeftChild = RightChild = NULL;
}
BinaryTreeNode(const T& e)
{
data = e;
LeftChild = RightChild = NULL;
}
BinaryTreeNode(const T& e,BinaryTreeNode *l,BinaryTreeNode* r)
{
data = e;
LeftChild = l;
RightChild = r;
}
private:
T data;
BinaryTreeNode<T>* LeftChild,
*RightChild;
};
template<class T>
class BinaryTree
{
public :
BinaryTree()
{
root = NULL;
}
void PreOrder(void (*Visit)(BinaryTreeNode<T>* u))
{
PreOrder(Visit,root);
}
int GetNodeCount()//新增了一个计算节点数的函数
{
nodeCount = 0;
PreOrder(ct);
return nodeCount;
}
private:
BinaryTreeNode<T> * root;
int nodeCount;
void PreOrder(void(*Visit)(BinaryTreeNode<T> * u),BinaryTreeNode<T> * t)
{
if(t)
{
Visit(t);
PreOrder(Visit,t->LeftChild);
PreOrder(Visit,t->RightChild);
}
}
void ct(BinaryTreeNode<T>* u)//原来的ct到这来了
{
nodeCount++;
}
};
int nodeCount = 0;
template<class T>
void ct(BinaryTreeNode<T>* u)
{
nodeCount++;
}
int main()
{
BinaryTree<int> tree;
//tree.PreOrder(ct);
tree.GetNodeCount();
return 0;
}
出现了下面的编译错误信息#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
template <class T>
class BinaryTreeNode
{
template <class type>
friend class BinaryTree;
public:
BinaryTreeNode()
{
LeftChild = RightChild = NULL;
}
BinaryTreeNode(const T& e)
{
data = e;
LeftChild = RightChild = NULL;
}
BinaryTreeNode(const T& e,BinaryTreeNode *l,BinaryTreeNode* r)
{
data = e;
LeftChild = l;
RightChild = r;
}
private:
T data;
BinaryTreeNode<T>* LeftChild,
*RightChild;
};
template<class T>
class BinaryTree
{
public :
BinaryTree()
{
root = NULL;
}
void PreOrder(void (*Visit)(BinaryTreeNode<T>* u))
{
PreOrder(Visit,root);
}
int GetNodeCount()//新增了一个计算节点数的函数
{
nodeCount = 0;
PreOrder(ct);
return nodeCount;
}
private:
BinaryTreeNode<T> * root;
int nodeCount;
void PreOrder(void(*Visit)(BinaryTreeNode<T> * u),BinaryTreeNode<T> * t)
{
if(t)
{
Visit(t);
PreOrder(Visit,t->LeftChild);
PreOrder(Visit,t->RightChild);
}
}
void ct(BinaryTreeNode<T>* u)//原来的ct到这来了
{
nodeCount++;
}
};
int nodeCount = 0;
template<class T>
void ct(BinaryTreeNode<T>* u)
{
nodeCount++;
}
int main()
{
BinaryTree<int> tree;
//tree.PreOrder(ct);
tree.GetNodeCount();
return 0;
}
希望各位给个解释,同时能够给予解决方案
怎样才能把ct函数写到类中呢?
只有本站会员才能查看附件,请 登录