# 求救！！！关于模板类的问题！在线等待！！！！！！！！！！！！！！！！！！！！

yonghker 2004-11-18 05:24:19
template <class T>
class A
{
T a;
}

#include "A.h"
template <class T>
class B
{ friend class A<T>;
A<T> b;

}

#include "B.h"
void C::f(void)
{
B<int> t;
}

...全文
87 点赞 收藏 2

2 条回复

yonghker 2004-11-19
A.h:
#pragma once
template <class T>
class BinaryNode
{
private:
T value;
BinaryNode<T>* lchild,rchild;

public:
BinaryNode<T>(void);

~BinaryNode<T>(void);
};

A.cpp:

#include "StdAfx.h"
#include ".\binarynode.h"
template <class T>
BinaryNode<T>::BinaryNode(void)
{

}
template <class T>

BinaryNode<T>::~BinaryNode(void)
{
}

B.h
:
#pragma once
#include "BinaryNode.h"
template <class T>
class BTree
{
friend class BinaryNode<T>;
public:
BTree(void);
~BTree(void);
private:
BinaryNode<T>* bt;

// 用先序遍历和中序遍历构造二叉树
void FindNode(T*,T*,BinaryNode<T>*);
public:
// 创建二叉树
void CreateBinaryTree(T* preTring, T* inTring);
};

B.cpp
:
#include "StdAfx.h"
#include ".\btree.h"
template <class T>
BTree<T>::BTree(void)
{

}
template <class T>
BTree<T>::~BTree(void)
{
}

// 用先序遍历和中序遍历构造二叉树
template<typename T>
void BTree<T>::FindNode(T*preTring,T*inTring,BinaryNode<T>*q)
{ if (preTring==null || inTring==null) return;

int i=0;
q->value=*preTring;
while (*inTring[i]!=*preTring) i++;
if (preTring[1]!=null) q->lchild=new BinaryNode<T>();
if (preTring[i+1]!=null) q->lchild=new BinaryNode<T>();
FindNode(preTring[1],inTring,q->lchild);
FindNode(preTring[i+1],inTring[i+1],q->rchild);

}

template<typename T>
void BTree<T>::CreateBinaryTree(T* preTring, T* inTring)
{

int m_preLen,m_inLen;
m_preLen=m_inLen=0;
while (preTring[m_preLen]!=null) m_preLen++;
while (inTring[m_inLen]!=null) m_inLen++;

if (m_preLen!=m_inLen) return;
if (m_preLen==0 || m_inLen==0) return;

FindNode(preTring,inTring,bt);
}
C.h:
#include "afxwin.h"

// CreateBTree 对话框

class CreateBTree : public CDialog
{
DECLARE_DYNAMIC(CreateBTree)

public:
CreateBTree(CWnd* pParent = NULL); // 标准构造函数
virtual ~CreateBTree();

// 对话框数据
enum { IDD = IDD_CREATEBTREE };

protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持

DECLARE_MESSAGE_MAP()
public:
// 类型控件
CComboBox comTypeCtl;
// 前序遍历控件
CEdit preTringCtl;
// 前序遍历序列
CString m_preStr;
// 中序遍历控件
CEdit inTringCtrl;
// 中序遍历序列
CString m_inString;
virtual BOOL OnInitDialog();
afx_msg void OnBnClickedOk();

};

C.cpp:
//

#include "stdafx.h"
#include "BinaryTree.h"
#include "reateBTree.h"
#include ".\reatebtree.h"
#include "BTree.h"
// CreateBTree 对话框

IMPLEMENT_DYNAMIC(CreateBTree, CDialog)
CreateBTree::CreateBTree(CWnd* pParent /*=NULL*/)
: CDialog(CreateBTree::IDD, pParent)
, m_preStr(_T(""))
, m_inString(_T(""))

{
}

CreateBTree::~CreateBTree()
{
}

void CreateBTree::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_NODETYPE, comTypeCtl);
DDX_Control(pDX, IDC_PRETRING, preTringCtl);
DDX_Text(pDX, IDC_PRETRING, m_preStr);
DDX_Control(pDX, IDC_INTRING, inTringCtrl);
DDX_Text(pDX, IDC_INTRING, m_inString);

}

BEGIN_MESSAGE_MAP(CreateBTree, CDialog)
ON_BN_CLICKED(IDOK, OnBnClickedOk)
END_MESSAGE_MAP()

// CreateBTree 消息处理程序

BOOL CreateBTree::OnInitDialog()
{
CDialog::OnInitDialog();

// TODO: 在此添加额外的初始化

return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}

void CreateBTree::OnBnClickedOk()
{
CString typeStr;

comTypeCtl.GetWindowText(typeStr);
BTree<T> a;//在此出错

}

zhy0101 2004-11-18

VC.NET

7540

.NET技术 VC.NET

2004-11-18 05:24