PCDVD數位科技討論區

PCDVD數位科技討論區 (https://www.pcdvd.com.tw/index.php)
-   疑難雜症區 (https://www.pcdvd.com.tw/forumdisplay.php?f=34)
-   -   可以幫我看看這條程式是錯什麼意思的嗎? (https://www.pcdvd.com.tw/showthread.php?t=502584)

jackyepson 2005-06-20 01:37 PM

可以幫我看看這條程式是錯什麼意思的嗎?
 
#include<stdio.h>
#include<stdlib.h>
struct llist
{
int num;
char na[10];
struct llist *next;
};
struct llist *createllist(struct llist *head)
{
struct llist *head;<<<<這條錯
struct llist *ptr,*ptr1;
int i;
head=(struct llist *)malloc(sizeof(struct llist));
printf("輸入姓名:");
scanf("%s",ptr->na);
printf("輸入編號:");
scanf("%d",ptr->num);
head->next=NULL;
ptr=head;
for(i=1;i<5;i++)
{
ptr1->next=(struct llist *)malloc(sizeof(struct llist));
printf("輸入姓名:");
scanf("%s",ptr->na);
printf("輸入編號:");
scanf("%d",ptr->num);
ptr1->next=NULL;
ptr->next=ptr1;
ptr=ptr->next;
}
}
struct llist *findnode(struct llist *head,int num)
{
struct llist *ptr;
ptr=head;
while(ptr!=NULL)
{
if(ptr->num==num)
return ptr;
ptr=ptr->next;
}
}
void main()
{
struct llist *head,*ptr;
int num;
head=createllist(head);
printf("郵寄編號:");
scanf("%d",&num);
if(num!=0)
{
ptr=findnode(head,num);
if(!ptr)
printf("沒有找到\n");
else
printf("姓名:%s\n",ptr->na);
}
}
如題囉!!
他說是Cpp1.cpp(11) : error C2082: redefinition of formal parameter 'head'
看不懂 :cry:

harrisonlin 2005-06-20 01:56 PM

createllist 這個函數已經傳入一個 llist 的 pointer 名曰 head,你又在裡面宣告一個 head,自然是"參數名稱重覆定義"啦!

jackyepson 2005-06-20 02:27 PM

ㄜ...有點不懂!!
是要改換成這樣嗎??
struct llist *createllist(struct llist *head2)
{
struct llist *head;

harrisonlin 2005-06-20 02:37 PM

引用:
作者jackyepson
ㄜ...有點不懂!!
是要改換成這樣嗎??
struct llist *createllist(struct llist *head2)
{
struct llist *head;


這樣子改應該可以修正你原本的得到的錯誤訊息.

有自己 compile 過一遍了嗎?

jackyepson 2005-06-20 02:46 PM

有啊!!可是還是不行Q_Q

harrisonlin 2005-06-20 02:58 PM

引用:
作者jackyepson
有啊!!可是還是不行Q_Q


所謂不行,是指錯誤訊息仍然是 redefinition of formal parameter 'head' ?

jackyepson 2005-06-20 03:01 PM

是多了警告,和一個錯
C:\Documents and Settings\123\桌面\電腦作業\Cpp1.cpp(32) : error C4716: 'createllist' : must return a value
C:\Documents and Settings\123\桌面\電腦作業\Cpp1.cpp(16) : warning C4700: local variable 'ptr' used without having been initialized
C:\Documents and Settings\123\桌面\電腦作業\Cpp1.cpp(23) : warning C4700: local variable 'ptr1' used without having been initialized
C:\Documents and Settings\123\桌面\電腦作業\Cpp1.cpp(43) : warning C4715: 'findnode' : not all control paths return a value
C:\Documents and Settings\123\桌面\電腦作業\Cpp1.cpp(48) : warning C4700: local variable 'head' used without having been initialized

harrisonlin 2005-06-20 03:16 PM

忽然發現我陷入了無限迴圈...:jolin:

你得到"錯誤"訊息的原因:函數 createllist 在宣告時就說了要傳回一個 llist 的指標,你的函數本體中並沒有明言什麼時候傳回,和傳回什麼東西,自然得此訊息.

你得到"警告"訊息的原因:那幾個區域變數都未經初始化(給定初值)就直接拿來使用, compiler 好心提醒你該初始化它們一下.

最後,和網友這樣子一來一回地討論這種只是因為語法錯誤而得到的錯誤訊息,並不是一個良好的互動方式,建議你找身旁的朋友或老師討教,才不會浪費了網路資源,和網友的耐心。

如果你無可避免地要用這種方式討論的話,最起碼可以做到"詳述問題"這一點,而不是一個"仍然無法編譯成功"的回應,讓人不知道你的編譯器到底告訴了你什麼...


所有的時間均為GMT +8。 現在的時間是11:25 PM.

vBulletin Version 3.0.1
powered_by_vbulletin 2026。