[問題] 該怎麼做才能正確的把標頭檔內的函式引入主程式內?

看板C_and_CPP (C/C++)作者 (卡瓦寧)時間15年前 (2010/11/13 20:45), 編輯推噓0(007)
留言7則, 3人參與, 最新討論串1/1
這是一個用C寫的多項式以鏈結串列來算相加和相乘 我把寫好的多相式相加、相乘、設定多項式、印出結果四個函式 分別以標頭檔.h匯入主程式 可是一直出現錯誤訊息 如果把這四個函式寫在同一個程式裡不分檔就不會有問題 請問我哪裡需要去修正呢? 想了好久都想不出問題在哪... 程式寫好一分檔卻不能跑實在很冏= = (下面我有用一個polyMultiply.h來舉例...麻煩各位幫我看看問題在哪?) #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <math.h> #include "createpoly.h"//設定多項式係數和次方的函式 #include "polyAdd.h"//多項式相加的函式 #include "polyMultiply.h"//多項式相乘的函式 #include "printpoly.h"//印出多項式的函式 typedef struct list { int coef; int exp; struct list *next; }node; void printpoly(node*);//宣告一個能印出多項式結果的函式 node *createpoly(int);//宣告一個能設定多項式的函式 node *polyAdd(node*, node*);//宣告一個能將兩個多項式相加的函式 node *polyMultiply(node*, node*);//宣告一個能將兩個多項式相乘的函式 int main() { node *poly1, *poly2, *add, *multiply; int keyin=0; int testCase; int caseCounter; scanf("%d",&testCase); for(caseCounter=1;caseCounter<=testCase;caseCounter++) { scanf("%d",&keyin); poly1=createpoly(keyin); scanf("%d",&keyin); poly2=createpoly(keyin); add=polyAdd(poly1, poly2); } printf("\n"); for(caseCounter=1;caseCounter<=testCase;caseCounter++) { multiply=polyMultiply(poly1,poly2); printf("Case%d:\n",caseCounter); printf("ADD\n"); printpoly(add); printf("MULTPLY\n"); printpoly(multiply); printf("END\n"); } system("pause"); return 0; /*標頭檔(用標頭檔polyMultiply舉例) #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <math.h> node *polyMultiply(node *poly1, node *poly2) { node *tail, *head, *ptr1, *ptr2, *ptrMultiply; ptr1=poly1; ptr2=poly2; head=tail=NULL; while(ptr1!=NULL && ptr2!=NULL) { ptrMultiply=malloc(sizeof(node)); if(ptr1->exp > ptr2->exp) { ptrMultiply->coef = ptr1->coef * ptr2->coef; ptrMultiply->exp = ptr1->exp + ptr2->exp; ptr1 = ptr1->next ; } else if(ptr1->exp < ptr2->exp) { ptrMultiply->coef = ptr2->coef * ptr1->coef; ptrMultiply->exp = ptr2->exp + ptr1->exp; ptr2 = ptr2->next ; } else { ptrMultiply->coef = ptr1->coef + ptr2->coef ; ptrMultiply->exp = ptr1->exp ; ptr1 = ptr1->next ; ptr2 = ptr2->next ; } ptrMultiply->next=NULL; if (head==NULL) head=ptrMultiply; else tail->next=ptrMultiply; tail=ptrMultiply; } if(ptr1==NULL) { while(ptr2!=NULL) { ptrMultiply=malloc(sizeof(node)); ptrMultiply->coef = ptr2->coef; ptrMultiply->exp = ptr2->exp; ptr2 = ptr2->next; tail->next = ptrMultiply; tail = ptrMultiply; } } else { while(ptr1!=NULL) { ptrMultiply=malloc(sizeof(node)); ptrMultiply->coef = ptr1->coef; ptrMultiply->exp = ptr1->exp; ptrMultiply = ptr1->next; tail->next = ptrMultiply; tail = ptrMultiply; } } return head; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.37.234.252

11/13 20:51, , 1F
順序有差喔...如果一起沒問題
11/13 20:51, 1F

11/13 20:51, , 2F
調一下順序 注意有沒有那種先用卻還沒有宣到的
11/13 20:51, 2F

11/14 00:10, , 3F
error沒貼不確定問題點 不過通常是 header的衛句沒加
11/14 00:10, 3F

11/14 00:10, , 4F
把header file改成.c 自己build看看就知道哪支.h 有問題
11/14 00:10, 4F

11/14 00:11, , 5F
如何避免header file問題? 有個說法很有好
11/14 00:11, 5F

11/14 00:12, , 6F
讓header file的相依性能夠自我滿足 改成.c build就知了
11/14 00:12, 6F

11/14 01:00, , 7F
已解決,謝謝
11/14 01:00, 7F
文章代碼(AID): #1CteY7IC (C_and_CPP)
文章代碼(AID): #1CteY7IC (C_and_CPP)