为了试一下链表的使用,我弄了下面这个程序,我的原意是想从键盘输入数据保存到txt文本中,然后再从文本读取,输出,可以编译的时候却出现了错误,具体的错误是这样的
error C2061: syntax error : identifier 'head'
error C2061: syntax error : identifier 'head'
请问有谁能跟我说一下为什么会出现这样的错误,先谢过了!
#include<iostream>
#include<fstream>
#define N sizeof(struct goods_message)
using namespace std;
struct goods_message
{ char name[10];
int point;
struct goods_message *next;
};
struct goods_message* head;
struct goods_message* write_message(struct goods_message* head2)
{int flag=1;
struct goods_message *p1,*p2;
p1=head2->next;
ofstream fp1("goods_message.txt");
while(flag)
{
p1=(struct goods_message*)malloc(N);
cout<<"请输入下一个信息"<<endl;
cin>>p1->name>>p1->point;
fp1.write((char*)p1,N);
p2->next=p1;
p2=p1;
cout<<"继续?(1:Y/0:N)"<<endl;
cin>>flag;
};
return p1;
}
struct goods_message* read_message(struct goods_message* head1)
{struct goods_message *p1;
p1=head1->next;
ifstream fp2("goods_message.txt");
while(p1!=NULL)
{
fp2.read((char*)p1,N);
cout<<p1->name<<p1->point<<endl;
p1=p1->next;
};
}
void main()
{
cout<<"请输入第一个信息:"<<endl;
head=(struct goods_message*)malloc(N);
cin>>head->name>>head->point;
struct goods_message* write_message(head);
struct goods_message* read_message(head);
}
里面有些东西还不是很完善,请大家不要见怪,还望大家能指点指点!