[問題] C語言外部參考變數

看板Programming作者 (把握當下)時間15年前 (2009/05/01 15:19), 編輯推噓1(101)
留言2則, 1人參與, 最新討論串1/1
編譯器: DevC++ A檔案程式: ====================================================================== #include <stdio.h> #include <stdlib.h> void report_count(); void accumulate(int k); int count=0; int main(void) { int value; register int i; //register variable printf("Enter a positive integer (0 to quit):"); while(scanf("%d",&value)==1 && value > 0) { ++count; for(i = value; i>=0; i--) accumulate(i); printf("Enter a positive integer(0 to quit):"); } report_count(); system("Pause"); return 0; } void report_count() { printf("Loop executed %d times\n",count); } ============================================================ B檔案程式: ============================================================ #include <stdio.h> #include <stdlib.h> extern int count; static int total=0; void accumulate(int k) { static int subtotal=0; if(k <= 0) { printf("loop cycle:%d\n",count); printf("subtotal:%d,total:%d\n",subtotal,total); subtotal=0; } else { subtotal += k; total += k; }; } ============================================================ 問題: 小弟再執行A程式的時候會有錯誤訊息 但是不知道錯在那邊 [Linker error] undefined reference to `accumulate(int)' 煩請會的大大幫忙解答 程式目的: B檔案會去參考到A檔案變數count,印出執行次數.. 從A檔案裡變數Value輸入一個值,然後呼叫accumulate function 印出結果.. -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 163.14.7.104 thank1984:轉錄至看板 C_and_CPP 05/01 15:21

05/02 01:18, , 1F
試試看把A檔案的accumulate function宣告
05/02 01:18, 1F

05/02 01:18, , 2F
改成 extern void accumulate(int k);
05/02 01:18, 2F
文章代碼(AID): #19-gAFkG (Programming)