[求助]帮我看看这段代码哪里错了?

palm_civet 2005-12-10 07:42:04
这是一个huaffman tree我用模板老出这种问题

--------------------Configuration: MinHeap - Win32 Debug--------------------
Compiling...
main.cpp
h:\minheapand huaffman\huaffmantree.h(54) : error C2530: 'firstchild' : references must be initialized
h:\minheapand huaffman\main.cpp(13) : while compiling class-template member function '__thiscall HuaffmanTree<char>::HuaffmanTree<char>(char [],int)'
h:\minheapand huaffman\huaffmantree.h(54) : error C2530: 'secondchild' : references must be initialized
h:\minheapand huaffman\main.cpp(13) : while compiling class-template member function '__thiscall HuaffmanTree<char>::HuaffmanTree<char>(char [],int)'
h:\minheapand huaffman\huaffmantree.h(56) : error C2146: syntax error : missing ',' before identifier 'hp'
h:\minheapand huaffman\main.cpp(13) : while compiling class-template member function '__thiscall HuaffmanTree<char>::HuaffmanTree<char>(char [],int)'
h:\minheapand huaffman\huaffmantree.h(56) : error C2065: 'hp' : undeclared identifier
h:\minheapand huaffman\main.cpp(13) : while compiling class-template member function '__thiscall HuaffmanTree<char>::HuaffmanTree<char>(char [],int)'
h:\minheapand huaffman\huaffmantree.h(56) : error C2143: syntax error : missing '>' before ';'
h:\minheapand huaffman\main.cpp(13) : while compiling class-template member function '__thiscall HuaffmanTree<char>::HuaffmanTree<char>(char [],int)'
h:\minheapand huaffman\huaffmantree.h(63) : error C2248: 'dataprt' : cannot access private member declared in class 'HuaffmanCodeNode<char>'
h:\minheapand huaffman\huaffmantree.h(21) : see declaration of 'dataprt'
h:\minheapand huaffman\main.cpp(13) : while compiling class-template member function '__thiscall HuaffmanTree<char>::HuaffmanTree<char>(char [],int)'
h:\minheapand huaffman\huaffmantree.h(64) : error C2248: 'data' : cannot access private member declared in class 'HuaffmanTreeNode<char>'
h:\minheapand huaffman\huaffmantree.h(10) : see declaration of 'data'
h:\minheapand huaffman\main.cpp(13) : while compiling class-template member function '__thiscall HuaffmanTree<char>::HuaffmanTree<char>(char [],int)'
h:\minheapand huaffman\huaffmantree.h(65) : error C2248: 'parent' : cannot access private member declared in class 'HuaffmanTreeNode<char>'
h:\minheapand huaffman\huaffmantree.h(11) : see declaration of 'parent'
h:\minheapand huaffman\main.cpp(13) : while compiling class-template member function '__thiscall HuaffmanTree<char>::HuaffmanTree<char>(char [],int)'
h:\minheapand huaffman\huaffmantree.h(65) : error C2248: 'leftChild' : cannot access private member declared in class 'HuaffmanTreeNode<char>'
h:\minheapand huaffman\huaffmantree.h(11) : see declaration of 'leftChild'
h:\minheapand huaffman\main.cpp(13) : while compiling class-template member function '__thiscall HuaffmanTree<char>::HuaffmanTree<char>(char [],int)'
h:\minheapand huaffman\huaffmantree.h(65) : error C2248: 'rightChild' : cannot access private member declared in class 'HuaffmanTreeNode<char>'
h:\minheapand huaffman\huaffmantree.h(11) : see declaration of 'rightChild'
h:\minheapand huaffman\main.cpp(13) : while compiling class-template member function '__thiscall HuaffmanTree<char>::HuaffmanTree<char>(char [],int)'
h:\minheapand huaffman\huaffmantree.h(67) : error C2228: left of '.MinHeap' must have class/struct/union type
h:\minheapand huaffman\main.cpp(13) : while compiling class-template member function '__thiscall HuaffmanTree<char>::HuaffmanTree<char>(char [],int)'
h:\minheapand huaffman\huaffmantree.h(68) : error C2374: 'i' : redefinition; multiple initialization
h:\minheapand huaffman\huaffmantree.h(61) : see declaration of 'i'
h:\minheapand huaffman\main.cpp(13) : while compiling class-template member function '__thiscall HuaffmanTree<char>::HuaffmanTree<char>(char [],int)'
h:\minheapand huaffman\huaffmantree.h(71) : error C2065: 'firstChild' : undeclared identifier
h:\minheapand huaffman\main.cpp(13) : while compiling class-template member function '__thiscall HuaffmanTree<char>::HuaffmanTree<char>(char [],int)'
h:\minheapand huaffman\huaffmantree.h(71) : error C2228: left of '.DeleteTop' must have class/struct/union type
h:\minheapand huaffman\main.cpp(13) : while compiling class-template member function '__thiscall HuaffmanTree<char>::HuaffmanTree<char>(char [],int)'
h:\minheapand huaffman\huaffmantree.h(72) : error C2065: 'secondChild' : undeclared identifier
h:\minheapand huaffman\main.cpp(13) : while compiling class-template member function '__thiscall HuaffmanTree<char>::HuaffmanTree<char>(char [],int)'
h:\minheapand huaffman\huaffmantree.h(72) : error C2228: left of '.DeleteTop' must have class/struct/union type
h:\minheapand huaffman\main.cpp(13) : while compiling class-template member function '__thiscall HuaffmanTree<char>::HuaffmanTree<char>(char [],int)'
h:\minheapand huaffman\huaffmantree.h(74) : error C2228: left of '.Insert' must have class/struct/union type
h:\minheapand huaffman\main.cpp(13) : while compiling class-template member function '__thiscall HuaffmanTree<char>::HuaffmanTree<char>(char [],int)'
h:\minheapand huaffman\huaffmantree.h(110) : error C2248: 'bit' : cannot access private member declared in class 'HuaffmanCodeNode<char>'
h:\minheapand huaffman\huaffmantree.h(19) : see declaration of 'bit'
h:\minheapand huaffman\main.cpp(13) : while compiling class-template member function 'void __thiscall HuaffmanTree<char>::display(void)'
h:\minheapand huaffman\huaffmantree.h(111) : error C2143: syntax error : missing ';' before '}'
h:\minheapand huaffman\main.cpp(13) : while compiling class-template member function 'void __thiscall HuaffmanTree<char>::display(void)'
执行 cl.exe 时出错.

main.obj - 1 error(s), 0 warning(s)
...全文
158 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
billjoy 2005-12-11
  • 打赏
  • 举报
回复
老大,书上的例题吧!
仔细看看再问!
palm_civet 2005-12-10
  • 打赏
  • 举报
回复
这里不是C C++区么?

怎么知道译器是否支持类的分开编译,我用的是VC 6

MinHeap<HuaffmanTreeNode<Type>> hp;//这个不对么?
h:\minheapand huaffman\huaffmantree.h(56) : error C2146: syntax error : missing ',' before identifier 'hp'
oyljerry 2005-12-10
  • 打赏
  • 举报
回复
引用初始化
happydivid 2005-12-10
  • 打赏
  • 举报
回复
又一个可能是编译器有问题的问题?
楼主检查自己的编译器是否支持类的分开编译先
cunsh 2005-12-10
  • 打赏
  • 举报
回复
楼长怎么把这个问题贴在C语言区呀?还说没人.
cunsh 2005-12-10
  • 打赏
  • 举报
回复
第一个错误就是声明了两个引用但是没有初始化.

你先做成普通的类试试看.再改成模板的.
palm_civet 2005-12-10
  • 打赏
  • 举报
回复
自己顶,怎么没人啊
palm_civet 2005-12-10
  • 打赏
  • 举报
回复


代码:

//main.cpp
# include <iostream.h>
# include "HuaffmanTree.h"

void main()
{
char *arr;
arr = new char[20];
cin >>arr;
HuaffmanTree <char> a(arr,sizeof(arr));
cout <<endl;
a.display();
}

//HuaffmanTree.h
# include "MinHeap.h"

const int MaxSize=20;
template <class Type> class HuaffmanTree;
template <class Type>
class HuaffmanTreeNode
{
friend class HuaffmanTree;
private:
Type data;
HuaffmanTreeNode<Type> *leftChild,*rightChild,*parent;
};

template <class Type>
class HuaffmanCodeNode
{
friend class HuaffmanTree;
private:
int bit[MaxSize];
int start;
HuaffmanTreeNode<Type> *dataprt;
public:
};

template <class Type>
class HuaffmanTree
{
private:
HuaffmanTreeNode<Type> *hfTree;
HuaffmanCodeNode<Type> *hfCode;
int currentSize;

public:
//HuaffmanTree();
HuaffmanTree(Type weight[],int n);
void HuaffmanCode();
void display();

protected:
void MergeTree(HuaffmanTreeNode<Type> &bt1,
HuaffmanTreeNode<Type> &bt2,HuaffmanTreeNode<Type> *pt)
{
pt->leftChild=&bt1;
pt->rightChild=&bt2;
pt->data=bt1.date+bt2.data;;
pt->parent=0;
bt1.parent=bt2.parent=pt;
}
};

template <class Type>
HuaffmanTree<Type>::HuaffmanTree(Type weight[],int n)
{
HuaffmanTreeNode <Type> &firstchild,&secondchild,*parent;
HuaffmanTreeNode <Type> *TNode;
MinHeap<HuaffmanTreeNode<Type>> hp;
if (n>MaxSize) cout <<"字符数太多" <<endl;
currentSize=n;
hfCode=new HuaffmanCodeNode<Type>[n];
TNode=new HuaffmanTreeNode <Type>[n];
for(int i=0;i<n;i++)
{
hfCode[i].dataprt=&TNode[i];
TNode[i].data=weight[i];
TNode[i].parent=TNode[i].leftChild=TNode[i].rightChild=0;
}
hp.MinHeap(TNode,n);
for(int i=0;i<n-1;i++)
{
parent=new HuaffmanTreeNode<Type>;
firstChild=hp.DeleteTop();
secondChild=hp.DeleteTop();
MergeTree(firstchild,secondchild,parent);
hp.Insert(parent);
}
hfTree=parent;
}

template <class Type>
void HuaffmanTree<Type>::HuaffmanCode()
{
HuaffmanCodeNode<Type> *cd=new HuaffmanCodeNode<Type>;
HuaffmanTreeNode<Type> *child;
HuaffmanTreeNode<Type> *parent;
for(int i=0;i<currentSize;i++)
{
cd->start=currentSize-1;
child=hfCode[i].dataptr;
parent=child->parent;
while(parent!=0)
{
if(parent->leftchild==child)
cd->bit[cd->start]=0;
else
cd->bit[bit->start]=1;
child=parent;
parent=parent->parent;
cd->streat--;
for(int k=0;k<currentSize;k++)
hfCode[i].bit[k]=cd->bit[k];
hfCode[i].start=cd->start;
}
}
}

template <class Type>
void HuaffmanTree<Type>::display()
{
for(int n=0;n<currentSize;n++)
cout <<hfCode->bit[n]
}


//MinHeap.h
template <class Type>
class MinHeap
{
private:
Type *heapArr;
int heapCurrentSize;
int heapMaxSize;
void FilterDown(int p);
void FilterUp(int p);

public:
MinHeap(){};
MinHeap(int maxSize);
MinHeap(Type a[], int n);
~MinHeap() {delete []heapArr;}
int Insert(const Type &d);
Type DeleteTop();
Type GetTop() const {return heapArr[0];}
int IsEmpty() const {return heapCurrentSize==heapMaxSize;}
int IsFull() const {return heapCurrentSize==heapMaxSize;}
int SizeofHeap() const {return heapCurrentSize;}
void SetEmpty() {heapCurrentSize=0;}
void display() {
for(int i=0;i<heapCurrentSize;i++) cout <<*(heapArr+i) <<" ";
}
};

template <class Type>
MinHeap<Type>::MinHeap(int maxSize)
{
if(maxSize<=0) cout <<"堆的大小不能小于1,堆未被创建"<<endl;
heapMaxSize=MaxSize;
heapArr=new Type[heapMaxSize];
heapCurrent=0;
}

template <class Type>
MinHeap<Type>::MinHeap(Type a[],int n)
{
if(n<=0) cout <<"堆的大小不能小于1,堆未被创建"<<endl;
heapMaxSize=n;
heapArr=new Type[heapMaxSize];
heapArr=a;
for(int k=0;k<n;k++)
*(heapArr+k)=a[k];
heapCurrentSize=n;
int i=(heapCurrentSize-2)/2;
while(i>=0) {
FilterDown(i);
i--;
}
}

template <class Type>
void MinHeap<Type>::FilterDown(const int start)
{
int i=start,j;
Type temp=heapArr[i];
j=2*i+1;
while(j<=heapCurrentSize-1)
{
if(j<heapCurrentSize-1&&heapArr[j]>heapArr[j+1])
j++;
if(temp<=heapArr[j]) break;
else
{
heapArr[i]=heapArr[j];
i=j;j=2*j+1;
}
}
heapArr[i]=temp;
}

template <class Type>
int MinHeap<Type>::Insert(const Type &d)
{
if(IsFull())
{
cout <<"堆已满" <<endl;
return 0;
}
heapArr[heapCurrentSize]=d;
FilterUp(heapCurrent);
heapCurrentSize++;
return 1;
}

template <class Type>
Type MinHeap<Type>::DeleteTop()
{
if(IsEmpty())
{
cout <<"堆已空" <<endl;
Type temp=heapArr[0];
heapArr[0]=heapArr[heapCurrentSize-1];
heapCurrentSize--;
FilterDown(0);
return temp;
}
}

-----------------------------------------------------------------------------------

但是如果我把HuaffmanTree类的实现部分放在HuaffmanTree.cpp中还有别的错误。

uaffmanTree.cpp
Linking...
main.obj : error LNK2001: unresolved external symbol "public: void __thiscall HuaffmanTree<char>::display(void)" (?display@?$HuaffmanTree@D@@QAEXXZ)
main.obj : error LNK2001: unresolved external symbol "public: __thiscall HuaffmanTree<char>::HuaffmanTree<char>(char * const,int)" (??0?$HuaffmanTree@D@@QAE@QADH@Z)
Debug/MinHeap.exe : fatal error LNK1120: 2 unresolved externals
执行 link.exe 时出错.

MinHeap.exe - 1 error(s), 0 warning(s)

求高手解答

69,374

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧