2013-09-05 82 views
1

最近再次拿起編程。我是一名初學者。我帶了一個課,但現在正在嘗試編譯和運行我在Flash上​​使用Dev C++在課堂上運行良好的程序。我現在正在家裏使用最新版本的Code :: Blocks。代碼庫缺失庫:blocks

下面是程序代碼一個簡單的計算器程序如下:

/* This program adds, subtracts, multiplies, and divides two integers. */ 

#include <stdio.h> 
#include <stdlib.h> 

// Function Declarations 
int getOption(void); 
void getData (int* a, int* b); 
float calc (int option, int num1, int num2); 

float add (float num1, float num2); 
float sub (float num1, float num2); 
float mul (float num1, float num2); 
divn (float num1, float num2); 

void printResult (float num1, float num2, float result, int option); 

int main (void) 
{ 
// Local Declarations 
int done = 0; 
int option; 
int num1; 
int num2; 
int result; 

// Statements 
while (!done) 
{ 
    option = getOption(); 
    if (option == 5) 
    done = 1; 
    else 
    { 
     do 
      { 
       printf("\n\nEnter two numbers: "); 
       scanf("%f %f", &num1, &num2); 
       if (option == 4 && num2 == 0) 

       { 
        printf("\a\n *** Error *** "); 
        printf("Second Number cannot be 0\n"); 
        } //if 

        } while (option == 4 && num2 == 0); 

        switch (option) 
        { 
         case 1: result = add (num1, num2); 
         break; 
         case 2: result = sub (num1, num2); 
         break; 
         case 3: result = mul (num1, num2); 
         break; 
         case 4: result = divn (num1, num2); 
         } // switch 

       printResult (num1, num2, result, option); 
      } // else option != 5 
     } // while 

     printf("\nThank you for using Calculator.\n"); 
     return 0; 
    } // main 

/* ========================= getOption =================================== 
    This function shows a menu and reads the user option. 
     Pre  nothing 
     Post returns a valid option */ 

int getOption (void) 
{ 
// Local Declarations 
int option; 

// Statements 
do 
{ 
    printf("\n******************"); 
    printf("\n*  Menu  *"); 
    printf("\n*    *"); 
    printf("\n* 1. ADD  *"); 
    printf("\n* 2. SUBTRACT *"); 
    printf("\n* 3. MULTIPLY *"); 
    printf("\n* 4. DIVIDE *"); 
    printf("\n* 5. QUIT  *"); 
    printf("\n*    *"); 
    printf("\n******************"); 

    printf("\n\n\nPlease type your choice "); 
    printf("and press the return key : "); 
    scanf("%d", &option); 

    if (option < 1 || option > 5); 
    printf("Invalid option. Please re-enter.\n"); 

} while (option < 1 || option > 5); 
    return option; 
} // getoption 

得到以下編譯錯誤,當我嘗試編譯:

C:\Users\Christopher\SkyDrive\School\Programming\Practice Stuff\complete calculator.o:complete calculator.c|| undefined reference to `add'| 
C:\Users\Christopher\SkyDrive\School\Programming\Practice Stuff\complete calculator.o:complete calculator.c|| undefined reference to `sub'| 
C:\Users\Christopher\SkyDrive\School\Programming\Practice Stuff\complete calculator.o:complete calculator.c|| undefined reference to `mul'| 
C:\Users\Christopher\SkyDrive\School\Programming\Practice Stuff\complete calculator.o:complete calculator.c|| undefined reference to `divn'| 
C:\Users\Christopher\SkyDrive\School\Programming\Practice Stuff\complete calculator.o:complete calculator.c|| undefined reference to `printResult'| 
||=== Build finished: 5 errors, 0 warnings (0 minutes, 0 seconds) ===| 

我相信錯誤是不是因爲代碼錯誤(我知道以前的代碼工作),但因爲我現在使用Code :: Blocks而不是Dev C++,所以我需要引用不同的庫,但不知道我需要哪個庫。

幫助將不勝感激。

+1

add,sub,mul,divn,printResult的_definitions_在哪裏?在另一個.CPP不在項目中? – Jarod42

回答

0

您可以清楚地看到add,sub,mul,divn,printResult被聲明但未定義。因此,爲了使用它們,您需要定義它們。此外,你的代碼是不準確的:

  1. 帶兩個INT作爲輸入,但浮動執行所有操作...
  2. 其次,上面請舉手不隨地定義功能,但繼續使用它們。
  3. 最後,聲明瞭一些函數(getOption,getData & calc),爲什麼不對算術函數和printResult做同樣的處理。

因此,這不是因爲缺少庫,而是因爲該函數已聲明但未定義。或者,如果出現這種情況,您可能需要鏈接另一個包含該定義的文件。

-Dany