c++头文件被重复定义,看下面程序?怎么解决?
//head.h#ifndefHEAD_H#defineHEAD_H#include<iostream>#include<fstream>#include<stdlib....
//head.h
#ifndef HEAD_H
#define HEAD_H
#include <iostream>
#include<fstream>
#include<stdlib.h>
#include<string.h>
using namespace std;
char raw[30][50];
int line=0;
struct word
{
char a[20];
struct word *next;
};
//类:句子
class article;
class sentence
{
public:
void input();
void circleoutput();
private:
struct word *head;
int count;
};
class article
{
public:
void insert();
void set();
void delet();
void show();
void showout();
void sort();
private:
sentence *p[10];
static int num;
};
int article::num=0;
#endif
//head.cpp
#include"head.h"
void sentence::input()
{
cout<<"qing shu ru ju zi"<<endl;
int i=0;
int flag=0;
char c;
struct word *p;
struct word *q;
head=new struct word;
q=head;
count=0;
cout<<"aa"<<endl;
while((c=getchar())!='\n')
{
//cout<<"aa"<<endl;
if(flag==0)
p=new struct word;
p->a[i++]=c;
flag=1;
if(c==' ')
{
count++;
p->a[i]='\0';
i=0;
flag=0;
q->next=p;
q=p;
}
}
cout<<"end;"<<endl;
q->next=head->next;
cout<<"end;"<<endl;
}
void sentence::circleoutput()
{
char t;
ofstream fout("a.txt",ios::ate);
if(!fout)
{
cout<<"file cannot open"<<endl;
}
int i,k;
struct word *p;
struct word *r;
p=head->next;
for(i=1;i<=count;i++)
{
r=p;
for(k=1;k<=count;k++)
{
cout<<(r->a);
fout<<(r->a);
r=r->next;
}
cout<<endl;
fout<<endl;
p=p->next;
}
FILE *fp;
if( (fp = fopen("a.txt", "r+")) == NULL )
{
printf("Error opening file " );
exit(1);
}
int s=0;
while((t=fgetc(fp))!=EOF)
{
if(t!='\n')
{
raw[line][s++]=t;
}
else
{
raw[line][s]='\0';
line++;
s=0;
//raw[line][s++]=' ';
}
}
}
void article::insert()
{
p[num]=new sentence;
p[num]->input();
num++;
cout<<num<<endl;
}
void article::delet()
{
int j;
int location;
cout<<"请输入删除的句子的行号:"<<"1~"<<num<<endl;
cin>>location;
if(location>num||location<1)
{
cout<<"对不起,删除的行号不在范围内"<<endl;
exit(0);
}
else
{
for(j=location-1;j<num-1;j++)
p[j]=p[j+1];
}
num--;
}
void article:: show()
{
cout<<"循环输出句子:"<<endl;
int j;
for(j=0;j<num;j++)
p[j]->circleoutput();
}
void article::showout()
{
//cout<<"haha"<<endl;
//cout<<line<<endl;
int j;
for(j=0;j<line;j++)
cout<<raw[j]<<endl;
}
void article::sort()
{
cout<<"排序并输出:"<<endl;
int m,j,k;
char s[30];
for(m=0;m<line-1;m++)
{
k=m;
for(j=m+1;j<line;j++)
{
if(strcmp(raw[k],raw[j])>0)
k=j;
}
if(k!=m)
{
strcpy(s,raw[m]);
strcpy(raw[m],raw[k]);
strcpy(raw[k],s);
}
}
showout();
}
void article::set()
{
cout<<"请输入第一句:"<<endl;
insert();
insert();insert();
/*for(t=0;t<10;t++)
{
wenzhang::insert();
cout<<"是否 展开
#ifndef HEAD_H
#define HEAD_H
#include <iostream>
#include<fstream>
#include<stdlib.h>
#include<string.h>
using namespace std;
char raw[30][50];
int line=0;
struct word
{
char a[20];
struct word *next;
};
//类:句子
class article;
class sentence
{
public:
void input();
void circleoutput();
private:
struct word *head;
int count;
};
class article
{
public:
void insert();
void set();
void delet();
void show();
void showout();
void sort();
private:
sentence *p[10];
static int num;
};
int article::num=0;
#endif
//head.cpp
#include"head.h"
void sentence::input()
{
cout<<"qing shu ru ju zi"<<endl;
int i=0;
int flag=0;
char c;
struct word *p;
struct word *q;
head=new struct word;
q=head;
count=0;
cout<<"aa"<<endl;
while((c=getchar())!='\n')
{
//cout<<"aa"<<endl;
if(flag==0)
p=new struct word;
p->a[i++]=c;
flag=1;
if(c==' ')
{
count++;
p->a[i]='\0';
i=0;
flag=0;
q->next=p;
q=p;
}
}
cout<<"end;"<<endl;
q->next=head->next;
cout<<"end;"<<endl;
}
void sentence::circleoutput()
{
char t;
ofstream fout("a.txt",ios::ate);
if(!fout)
{
cout<<"file cannot open"<<endl;
}
int i,k;
struct word *p;
struct word *r;
p=head->next;
for(i=1;i<=count;i++)
{
r=p;
for(k=1;k<=count;k++)
{
cout<<(r->a);
fout<<(r->a);
r=r->next;
}
cout<<endl;
fout<<endl;
p=p->next;
}
FILE *fp;
if( (fp = fopen("a.txt", "r+")) == NULL )
{
printf("Error opening file " );
exit(1);
}
int s=0;
while((t=fgetc(fp))!=EOF)
{
if(t!='\n')
{
raw[line][s++]=t;
}
else
{
raw[line][s]='\0';
line++;
s=0;
//raw[line][s++]=' ';
}
}
}
void article::insert()
{
p[num]=new sentence;
p[num]->input();
num++;
cout<<num<<endl;
}
void article::delet()
{
int j;
int location;
cout<<"请输入删除的句子的行号:"<<"1~"<<num<<endl;
cin>>location;
if(location>num||location<1)
{
cout<<"对不起,删除的行号不在范围内"<<endl;
exit(0);
}
else
{
for(j=location-1;j<num-1;j++)
p[j]=p[j+1];
}
num--;
}
void article:: show()
{
cout<<"循环输出句子:"<<endl;
int j;
for(j=0;j<num;j++)
p[j]->circleoutput();
}
void article::showout()
{
//cout<<"haha"<<endl;
//cout<<line<<endl;
int j;
for(j=0;j<line;j++)
cout<<raw[j]<<endl;
}
void article::sort()
{
cout<<"排序并输出:"<<endl;
int m,j,k;
char s[30];
for(m=0;m<line-1;m++)
{
k=m;
for(j=m+1;j<line;j++)
{
if(strcmp(raw[k],raw[j])>0)
k=j;
}
if(k!=m)
{
strcpy(s,raw[m]);
strcpy(raw[m],raw[k]);
strcpy(raw[k],s);
}
}
showout();
}
void article::set()
{
cout<<"请输入第一句:"<<endl;
insert();
insert();insert();
/*for(t=0;t<10;t++)
{
wenzhang::insert();
cout<<"是否 展开
3个回答
展开全部
在头文件中只申明,不要定义
像这些
char raw[30][50];
int line=0;
统统这样定义
extern char raw[30][50];
extern int line;
然后在某个c.pp文件中去定义
在头文件中只申明,不要定义
像这些
char raw[30][50];
int line=0;
像这些
char raw[30][50];
int line=0;
统统这样定义
extern char raw[30][50];
extern int line;
然后在某个c.pp文件中去定义
在头文件中只申明,不要定义
像这些
char raw[30][50];
int line=0;
追问
main.obj : error LNK2005: "int line" (?line@@3HA) already defined in head.obj
main.obj : error LNK2005: "private: static int article::num" (?num@article@@0HA) already defined in head.obj
main.obj : error LNK2005: "char (* raw)[50]" (?raw@@3PAY0DC@DA) already defined in head.obj
Debug/kwic.exe : fatal error LNK1169: one or more multiply defined symbols found
追答
#ifndef HEAD_H
#define HEAD_H
#include
#include
#include
#include
using namespace std;
extern char raw[30][50];//char raw[30][50]; .c中定义
extern int line;//int line=0; ;
struct word
{
char a[20];
struct word *next;
};
//类:句子
class article;
class sentence
{
public:
void input();
void circleoutput();
private:
struct word *head;
int count;
};
class article
{
public:
void insert();
void set();
void delet();
void show();
void showout();
void sort();
private:
sentence *p[10];
static int num;
};
///int article::num=0; c 中定义
#endif
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
如果是重复定义,那么去掉重复定义就好了。你用的是什么IDE?一般IDE都会检查重复定义的啊。IDE是非常重要的,好的IDE,可以帮助你检查并修正这些错误。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把
int article::num=0;
放到文件head.cpp里面去定义。
int article::num=0;
放到文件head.cpp里面去定义。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询