+ -
当前位置:首页 → 问答吧 → C++重复定义的错误,没有头绪,MFC

C++重复定义的错误,没有头绪,MFC

时间:2011-12-04

来源:互联网

nput.obj : error LNK2005: "int n" (?n@@3HA) already defined in HaffCode.obj
Input.obj : error LNK2005: "struct HaffNode * ht" (?ht@@3PAUHaffNode@@A) already defined in HaffCode.obj
Input.obj : error LNK2005: "struct HaffCode * hc" (?hc@@3PAUHaffCode@@A) already defined in HaffCode.obj
Input.obj : error LNK2005: "char * a" (?a@@3PADA) already defined in HaffCode.obj
Input.obj : error LNK2005: "int * w" (?w@@3PAHA) already defined in HaffCode.obj
Debug/Haffman.exe : fatal error LNK1169: one or more multiply defined symbols found执行 link.exe 时出错.

我查找过,没有重复定义,在类HaffCode.h中定义了全局变量,代码如下,哪位high hand 有想法处理这个问题的,给我解答解答,可能是我的struct(结构)处理的不科学。
HaffCode.h的函数如下:

struct HaffNode
{
int weight;
int parent;
int lchild;
int rchild;
};
struct HaffCode
{
int *bit;
int start;
int weight;
char data;
};
int n;
HaffNode *ht;
HaffCode *hc;
char *a;//存放元素
int *w;//存放权重

void buildfile(int w[],char a[],int n);
void Haffman(int w[],int n,HaffNode ht[],HaffCode hc[],char a[]);


HaffCode.cpp如下,供参考:
#include "stdafx.h"
#include "Haffman.h"
#include "HaffCode.h"
#include<iostream>
#include<fstream>
#include<stdlib.h>
using namespace std;

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

void Haffman(int w[],int n,HaffNode ht[],HaffCode hc[],char a[])
{
int i,j,m1,m2,x1,x2;
for(i=0;i<2*n-1;i++)
{
if(i<n)
ht[i].weight=w[i];
else
ht[i].weight=0;
ht[i].parent=0;
ht[i].lchild=-1;
ht[i].rchild=-1;
}
for(i=0;i<n;i++)
hc[i].data=a[i];
for(i=0;i<n-1;i++)
{
m1=m2=1000;
x1=x2=0;
for(j=0;j<n+i;j++)
{
if(ht[j].weight<m1&&ht[j].parent==0)
{
m2=m1;
x2=x1;
m1=ht[j].weight;
x1=j;
}
else
if(ht[j].weight<m2&&ht[j].parent==0)
{
m2=ht[j].weight;
x2=j;
}
}
ht[x1].parent=n+i;
ht[x2].parent=n+i;
ht[n+i].weight=ht[x1].weight+ht[x2].weight;
ht[n+i].lchild=x1;
ht[n+i].rchild=x2;
}
//由哈夫曼树生成哈夫曼编码
HaffCode cd;
cd.bit=new int[n];
int child,parent;
for(i=0;i<n;i++)
{
cd.start=n-1;
cd.weight=ht[i].weight;

child=i;
parent=ht[child].parent;
while(parent!=0)
{
if(ht[parent].lchild==child)
cd.bit[cd.start]=0;
else
cd.bit[cd.start]=1;
cd.start--;
child=parent;
parent=ht[child].parent;
}
hc[i].bit=new int[n];
for(j=cd.start+1;j<n;j++)
hc[i].bit[j]=cd.bit[j];
hc[i].start=cd.start;
hc[i].weight=cd.weight;
}
}


void buildfile(int w[],char a[],int n)//文件写入
{
ofstream fop("hfmTree.txt");
for(int i=0;i<n;i++)
{
fop<<a[i]<<" "<<w[i]<<endl;
flush(cout);
}
ifstream fip("hfmTree.txt");
fip.close();
}

作者: CarbonComputer   发布时间: 2011-12-04

#ifndefine HAFFCODE_H_2011124194517
#define HAFFCODE_H_2011124194517

#endif // HAFFCODE_H_2011124194517

作者: icechenbing   发布时间: 2011-12-04

HaffCode.h中,放入声明
extern int n;
extern HaffNode *ht;
extern HaffCode *hc;
extern char *a;//存放元素
extern int *w;//存放权重
然后在HaffCode.cpp中,放入定义
int n;
HaffNode *ht;
HaffCode *hc;
char *a;//存放元素
int *w;//存放权重

作者: keiy   发布时间: 2011-12-04

请不要在头文件定义变量……

楼主沿着这个思路去google,很快就能明白,有所收获了……


解决方法:
h文件中的下面部分放入HaffCode.cpp
int n;
HaffNode *ht;
HaffCode *hc;
char *a;//存放元素
int *w;//存放权重

h文件对应部分写成
extern int n;
extern HaffNode *ht;
extern HaffCode *hc;
extern char *a;//存放元素
extern int *w;//存放权重


作者: mstlq   发布时间: 2011-12-04