功能原型问题

时间:2015-12-11 03:37:08

标签: c

感谢大家的建议!我不再在switch语句中使用该函数,但现在它给了我致命的错误。有人知道它为什么现在不能编译吗?我还在原型设计或定义错误吗?再次感谢!

uhunix:/home04/y/yingkit/ee150% gcc functions2.c 
Undefined                       first referenced
symbol                             in file
largest_of_three                    /var/tmp//ccEk23i2.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status

这是整个代码:

#include <stdio.h>
#include <math.h>
//include macro functions if any


int main(void)
{
   int repeat = 1;
   int option = 0;   

   while(repeat == 1)
   {   
      printf("\nOPTIONS:\n\n");
      printf("1. Find the largest of three numbers.\n");
      printf("2. Calculate the factorial of a number.\n");
      printf("3. Truncate a number.\n");
      printf("4. Round a number.\n");
      printf("5. Find the inverse of a number.\n");
      printf("0. Exit the program.\n\n");
      printf("What do you want to do\?\n");

      scanf("%i", &option);

      float largest_of_three(float, float, float); //prototype

      if(option == 0)
      {    
         break;
      }

      if(option == 1)
      {

            float x = 0;
            float y = 0;
            float z = 0;
            float result = 0;

            printf("First number: ");
            scanf("%f", &x);
            printf("Second number: ");
            scanf("%f", &y);
            printf("Third number: ");
            scanf("%f", &z);   

            result = largest_of_three(x, y, z); //calling

            float largest_of_three(float x, float y, float z)
            {
               float w = 0;
               if(x > y && x > z)
               {
                  w = x;
               }
               else
               {
                  if(y > x && y > z)
                  {
                     w = y;
                  }   
                  else //middle
                  {
                     if(z > x && z > y)
                     {
                        w = z;
                     }
                     else  
                     {
                        printf("There is no single greatest number.\n");
                     }
                  } //end middle else
               } //end outer else
               printf("The greatest number is %f.", w);
               return w;
            } //end largest_of_three function
      } //end option 1
      getchar( ); //to prevent buffer issues
      printf("Would you like to do another operation\? Type y for yes, n for no.\n");

      char yesno = 'y';
      scanf("%c", &yesno);
      if(yesno == 'y' || yesno == 'Y')
      {
         repeat = 1;
      }
      else
      {
         repeat = 0;
      }


   } //end while loop 

   return 0; 
}           

1 个答案:

答案 0 :(得分:1)

第57行的代码是非法的。可能未在ISO C中的其他函数内定义函数。

错误表明您的编译器有一个扩展来允许嵌套函数,为它们提供内部链接。但是你给出了一个带有外部链接的原型,不匹配。

要解决此问题,请停止使用嵌套函数。 (您可能可以通过将static添加到原型的开头来修复它,但这是错误的编码风格。)