頭檔案
#ifndef T3X_H
#define T3X_H
#include<iostream>
#include<string.h>
using namespace std;
struct StuType;
struct LNode;
extern LinkList InitLink();
extern LinkList LinkInsert(LinkList head,int n);
extern int LinkDelete(LinkList head,int n);
extern LinkList LinkLocate(LinkList head,int n);
extern int PrintAll(LinkList head);
extern int Add();
extern int ListLength(LinkList head);
#endif
.cpp
#include"t3x.h"
#include<iostream>
#include<string>
using namespace std;
struct StuType
{
string num;
string name;
int age;
};
struct LNode
{
StuType data;
struct LNode *next;
}LNode,*LinkList;
LinkList InitLink()
{ LinkList head;
head=new LinkList;
if (head==NULL)
{
printf("申請記憶體空間失敗\n");
}
head->next=NULL;
return (head);
}
LinkList LinkInsert(LinkList head,int n)
{
for(int i=0;i<n;i++)
{ head=head->next; }
newnode=new LNode;
cin>>newnode.num;
cin>>newnode.name;
cin>>newnode.age;
return(head);
}
int LinkDelete(LinkList head,int n)
{
LinkList p,q;
p=head;
for(int i=0;i<n;i++)
{ p=p->next; }
q=p->next;
p->next=q->next;
return 0;
}
LinkList LinkLocate(LinkList head,int n)
{
LinkList p;
p=head;
for(int i=0;i<n;i++)
{ p=p->next; }
return p->data;
}
int PrintAll(LinkList head)
{
LinkList p=head;
while(p!=NULL)
{
cout<<"學號:"<<p->num<<' '<<"姓名:"<<p->name<<' '<<"年齡:"<<p->age<<endl;
p=p->next;
}
return 0;
}
int Add()
{
cout<<"請輸入學號、姓名和專業";
newLNode=new LNode;
cin>>newLNode->num;
cin>>newLNode->name;
cin>>newLNode->age;
return 0;
}
int ListLength(LinkList head)
{
int n;
while(head!=NULL)
{
head=head->next;
n=n+1;
}
return n;
}
int main()
{ int i,j,n,func;
LNode data;
LinkList *pdata;
pdata=https://bbs.csdn.net/topics/InitLink();
cout<<"請輸入要輸入的學生資訊條數";
cin>>n;
for(i=0;i<n;i++)
int Add();
cout<<"單鏈表中的學生資訊為:" <<endl;
PrintAll(pdata);
printf("0.退出,\t1.插入資訊,\t2.洗掉資訊,\t3.更新指定學生的姓名,\t4.查找指定學生的資訊,\t5.輸出所有學生資訊,\t6.輸出學生數量\n");
for(j=0;;j++)
{
scanf("%d",&func);
switch(func)
{
case 0:
exit(1);
break;
case 1:
cout<<"請輸入要插入資訊的位置:";
cin>>i;
cout<<"請輸入第"<<i<<"號學生的num,name,以及age"<<endl;
cin>>data.num>>data.name>>data.age;
if(LinkInsert(pdata,i))
{ cout<<"插入資料成功"<<endl;
PrintAll(pdata); }
break;
case 2:
cout<<"請輸入要洗掉資訊的位置:";
cin>>i;
if(LinkDelete(pdata,i))
{ cout<<"資料洗掉成功"<<endl;
PrintAll(pdata); }
break;
case 3:
cout<<"請輸入要更新資訊的位置:";
cin>>i;
data=https://bbs.csdn.net/topics/LinkLocate(pdata,i);
cout<<"請輸入替換的姓名:";
cin>>data.name;
cout<<"更新后第"<<i<<"個學生的資訊為:num:"<<data.num<<",name:"<<data.name<<",age:"<<data.age<<endl;
break;
case 4:
cout<<"請輸入要查找資訊的位置:";
cin>>i;
data=https://bbs.csdn.net/topics/LinkLocate(pdata,i);
cout<<"第"<<i<<"個學生的資訊為:num:"<<data.num<<",name:"<<data.name<<",age:"<<data.age<<endl;
break;
case 5:
PrintAll(pdata);
break;
case 6:
cout<<"學生數量為"<<ListLength(pdata);
}
}
return 0;
}
然后老是出現下圖的錯誤,但是不知道該怎么改

uj5u.com熱心網友回復:
把CPP里面那兩個結構體定義覆寫到.h中uj5u.com熱心網友回復:
C++編譯器編譯程式的時候,先編譯頭檔案,之后再編譯cpp源檔案。在你的頭檔案中使用了LinkList,但是LinkList是在cpp源檔案中定義的,所以編譯器不認識LinkList。應該把cpp中的struct StuType
{
string num;
string name;
int age;
};
struct LNode
{
StuType data;
struct LNode *next;
}LNode,*LinkList;
寫入到頭檔案中。如果非要把這兩個結構體在cpp中宣告,請參考《C++中類的前向宣告》,希望能幫到您!
uj5u.com熱心網友回復:

這樣修改了以后,為什么第一個錯誤還是存在呢?
ps:程式沒有改變,只是修改了位置,從cpp到頭檔案
uj5u.com熱心網友回復:
你的頭檔案的第二個結構體前面加一個typedef
uj5u.com熱心網友回復:

這樣以后就瘋狂報錯了XD
c++和c中的typedef是有一點不一樣的吧?
uj5u.com熱心網友回復:
這樣修改了以后,為什么第一個錯誤還是存在呢?
ps:程式沒有改變,只是修改了位置,從cpp到頭檔案
你的頭檔案的第二個結構體前面加一個typedef
這樣以后就瘋狂報錯了XD
c++和c中的typedef是有一點不一樣的吧?
你把你的代碼發給我 頭檔案和CPP檔案 我幫你編譯看看 私信可以發檔案好像
uj5u.com熱心網友回復:
頭檔案#include<iostream>
#include<string.h>
using namespace std;
struct StuType;
struct LNode;
struct StuType
{
string num;
string name;
int age;
};
typedef struct LNode
{
StuType data;
struct LNode *next;
}LNode, *LinkList;
extern LinkList InitLink();
extern LinkList LinkInsert(LinkList head, int n);
extern int LinkDelete(LinkList head, int n);
extern LinkList LinkLocate(LinkList head, int n);
extern int PrintAll(LinkList head);
extern int Add();
extern int ListLength(LinkList head);
源檔案
LinkList InitLink()
{
LinkList head;
head = new LNode();
if (head == NULL)
{
printf("申請記憶體空間失敗\n");
}
head->next = NULL;
return (head);
}
LinkList LinkInsert(LinkList head, int n)
{
for (int i = 0; i<n; i++)
{
head = head->next;
}
LinkList newnode = new LNode();
cin >> newnode->data.num;
cin >> newnode->data.name;
cin >> newnode->data.age;
return(head);
}
int LinkDelete(LinkList head, int n)
{
LinkList p, q;
p = head;
for (int i = 0; i<n; i++)
{
p = p->next;
}
q = p->next;
p->next = q->next;
return 0;
}
LinkList LinkLocate(LinkList head, int n)
{
LinkList p;
p = head;
for (int i = 0; i<n; i++)
{
p = p->next;
}
return p;
}
int PrintAll(LinkList head)
{
LinkList p = head;
while (p != NULL)
{
cout << "學號:" << p->data.num << ' ' << "姓名:" << p->data.name << ' ' << "年齡:" << p->data.age << endl;
p = p->next;
}
return 0;
}
int Add()
{
cout << "請輸入學號、姓名和專業";
LinkList newLNode = new LNode();
cin >> newLNode->data.num;
cin >> newLNode->data.name;
cin >> newLNode->data.age;
return 0;
}
int ListLength(LinkList head)
{
int n = 0;
while (head != NULL)
{
head = head->next;
n = n + 1;
}
return n;
}
int main()
{
int i, j, n, func;
LNode data;
LinkList pdata;
pdata = InitLink();
cout << "請輸入要輸入的學生資訊條數";
cin >> n;
for (i = 0; i<n; i++)
int Add();
cout << "單鏈表中的學生資訊為:" << endl;
PrintAll(pdata);
printf("0.退出,\t1.插入資訊,\t2.洗掉資訊,\t3.更新指定學生的姓名,\t4.查找指定學生的資訊,\t5.輸出所有學生資訊,\t6.輸出學生數量\n");
for (j = 0;; j++)
{
scanf("%d", &func);
switch (func)
{
case 0:
exit(1);
break;
case 1:
cout << "請輸入要插入資訊的位置:";
cin >> i;
cout << "請輸入第" << i << "號學生的num,name,以及age" << endl;
cin >> data.data.num >> data.data.name >> data.data.age;
if (LinkInsert(pdata, i))
{
cout << "插入資料成功" << endl;
PrintAll(pdata);
}
break;
case 2:
cout << "請輸入要洗掉資訊的位置:";
cin >> i;
if (LinkDelete(pdata, i))
{
cout << "資料洗掉成功" << endl;
PrintAll(pdata);
}
break;
case 3:
cout << "請輸入要更新資訊的位置:";
cin >> i;
data = *LinkLocate(pdata, i);
cout << "請輸入替換的姓名:";
cin >> data.data.name;
cout << "更新后第" << i << "個學生的資訊為:num:" << data.data.num << ",name:" << data.data.name << ",age:" << data.data.age << endl;
break;
case 4:
cout << "請輸入要查找資訊的位置:";
cin >> i;
data = *LinkLocate(pdata, i);
cout << "第" << i << "個學生的資訊為:num:" << data.data.num << ",name:" << data.data.name << ",age:" << data.data.age << endl;
break;
case 5:
PrintAll(pdata);
break;
case 6:
cout << "學生數量為" << ListLength(pdata);
}
}
return 0;
}
VS2015 C++環境
uj5u.com熱心網友回復:
頭檔案
#include<iostream>
#include<string.h>
using namespace std;
struct StuType;
struct LNode;
struct StuType
{
string num;
string name;
int age;
};
typedef struct LNode
{
StuType data;
struct LNode *next;
}LNode, *LinkList;
extern LinkList InitLink();
extern LinkList LinkInsert(LinkList head, int n);
extern int LinkDelete(LinkList head, int n);
extern LinkList LinkLocate(LinkList head, int n);
extern int PrintAll(LinkList head);
extern int Add();
extern int ListLength(LinkList head);
源檔案
LinkList InitLink()
{
LinkList head;
head = new LNode();
if (head == NULL)
{
printf("申請記憶體空間失敗\n");
}
head->next = NULL;
return (head);
}
LinkList LinkInsert(LinkList head, int n)
{
for (int i = 0; i<n; i++)
{
head = head->next;
}
LinkList newnode = new LNode();
cin >> newnode->data.num;
cin >> newnode->data.name;
cin >> newnode->data.age;
return(head);
}
int LinkDelete(LinkList head, int n)
{
LinkList p, q;
p = head;
for (int i = 0; i<n; i++)
{
p = p->next;
}
q = p->next;
p->next = q->next;
return 0;
}
LinkList LinkLocate(LinkList head, int n)
{
LinkList p;
p = head;
for (int i = 0; i<n; i++)
{
p = p->next;
}
return p;
}
int PrintAll(LinkList head)
{
LinkList p = head;
while (p != NULL)
{
cout << "學號:" << p->data.num << ' ' << "姓名:" << p->data.name << ' ' << "年齡:" << p->data.age << endl;
p = p->next;
}
return 0;
}
int Add()
{
cout << "請輸入學號、姓名和專業";
LinkList newLNode = new LNode();
cin >> newLNode->data.num;
cin >> newLNode->data.name;
cin >> newLNode->data.age;
return 0;
}
int ListLength(LinkList head)
{
int n = 0;
while (head != NULL)
{
head = head->next;
n = n + 1;
}
return n;
}
int main()
{
int i, j, n, func;
LNode data;
LinkList pdata;
pdata = InitLink();
cout << "請輸入要輸入的學生資訊條數";
cin >> n;
for (i = 0; i<n; i++)
int Add();
cout << "單鏈表中的學生資訊為:" << endl;
PrintAll(pdata);
printf("0.退出,\t1.插入資訊,\t2.洗掉資訊,\t3.更新指定學生的姓名,\t4.查找指定學生的資訊,\t5.輸出所有學生資訊,\t6.輸出學生數量\n");
for (j = 0;; j++)
{
scanf("%d", &func);
switch (func)
{
case 0:
exit(1);
break;
case 1:
cout << "請輸入要插入資訊的位置:";
cin >> i;
cout << "請輸入第" << i << "號學生的num,name,以及age" << endl;
cin >> data.data.num >> data.data.name >> data.data.age;
if (LinkInsert(pdata, i))
{
cout << "插入資料成功" << endl;
PrintAll(pdata);
}
break;
case 2:
cout << "請輸入要洗掉資訊的位置:";
cin >> i;
if (LinkDelete(pdata, i))
{
cout << "資料洗掉成功" << endl;
PrintAll(pdata);
}
break;
case 3:
cout << "請輸入要更新資訊的位置:";
cin >> i;
data = *LinkLocate(pdata, i);
cout << "請輸入替換的姓名:";
cin >> data.data.name;
cout << "更新后第" << i << "個學生的資訊為:num:" << data.data.num << ",name:" << data.data.name << ",age:" << data.data.age << endl;
break;
case 4:
cout << "請輸入要查找資訊的位置:";
cin >> i;
data = *LinkLocate(pdata, i);
cout << "第" << i << "個學生的資訊為:num:" << data.data.num << ",name:" << data.data.name << ",age:" << data.data.age << endl;
break;
case 5:
PrintAll(pdata);
break;
case 6:
cout << "學生數量為" << ListLength(pdata);
}
}
return 0;
}
VS2015 C++環境
謝謝~不過我把您的上述代碼放到我常用的Visual C++ 6.0的時候會出現這種錯誤——

不是很明白這里怎么了orz
uj5u.com熱心網友回復:
這樣修改了以后,為什么第一個錯誤還是存在呢?
ps:程式沒有改變,只是修改了位置,從cpp到頭檔案
你的頭檔案的第二個結構體前面加一個typedef
這樣以后就瘋狂報錯了XD
c++和c中的typedef是有一點不一樣的吧?
你把你的代碼發給我 頭檔案和CPP檔案 我幫你編譯看看 私信可以發檔案好像
謝謝~不過私信好像發不了檔案,頭檔案和cpp的代碼就是帖子最上面的兩個,我用的是Visual C++ 6.0
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/183358.html
標籤:C++ 語言
上一篇:幫我看看這個題
