缺少代码库:块

时间:2013-09-05 16:31:39

标签: c++ compiler-errors libraries

最近再次选择编程。我是初学者。我一段时间拿了一个课,但我现在正在尝试编译并运行一个程序,我在flash上​​使用Dev C ++在课堂上运行良好。我现在在家里使用最新版本的Code :: 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 个答案:

答案 0 :(得分:0)

您可以清楚地看到add,sub,mul,divn,printResult已声明但未定义。因此,为了使用它们,您需要定义它们。此外,您的代码不准确:

  1. 输入两个 int ,但是在 float ...
  2. 上执行所有操作
  3. 其次,如上所述,您不会在任何地方定义您的功能,而是继续使用它们。
  4. 最后,定义了一些声明的函数( getOption,getData&amp; calc ),为什么不对算术函数和printResult执行相同的操作。
  5. 因此,它不是因为缺少库而是因为声明了函数但未定义。或者,如果是这种情况,您可能需要链接包含该定义的另一个文件。

    -Dany