错误C2371,重新定义;不同的基本类型

时间:2014-11-30 17:56:44

标签: c

我得到错误代码c2371,用于函数;分开,最长和最短。我认为它是关于输入参数的。

error C2371: 'seperate' : redefinition; different basic types
error C2371: 'shortest' : redefinition; different basic types
error C2371: 'longest' : redefinition; different basic types

代码:

#include <stdio.h>

int main(void)
{
  char msg[41];
  int selector = 0;

  printf("Alinur Caglayan - 18050127622 - Lab Work 5 ");
  printf("Please enter a string message with maximum of 40 characters: ");
  gets(msg);

  printf("Please select one of the following functions:\n1) Longest Word Function");
  printf("\n2) Shortest Word Function\n3) Separate Words Function\n4) Exit: ");
  scanf("%d", &selector);

  if(selector == 1)
  {
    longest(msg);
  }
  else if(selector == 2)
  {
    shortest();
  }
  else if(selector == 3)
  {
    seperate();
  }
  else if(selector == 4)
  {
    return 0;
  }
  else
  {
    printf("\nPlease enter a valid integer!\n");
    scanf("%d", selector);
  }
}

char longest(msg)
{
  char *temp = msg, *word = msg, *max = msg;
  int increment = 0, incrementmax = 0;

  do {
    if(!(*temp & ~32)) 
    {
      if(increment > incrementmax) 
      {
        incrementmax = increment;
        max = word;
      }
      increment = 0;
    } 
    else if(!increment++) 
    {
      word = temp;
    }
  } 
  while(*temp++ != 0);

  for(temp = max; (*temp & ~32); ) 
  {
    printf("%c", *temp++);
  }
}

char shortest(msg)
{
  char *temp = msg, *word = msg, *max = msg;
  int increment = 0, incrementmax = 0;

  do {

    if(!(*temp & ~32)) 
    {

      if(increment > incrementmax) 
      {
        incrementmax = increment;
        max = word;
      }
      increment = 0;
    } 
    else if(!increment++)
    {
      word = temp; 
    }
  } 
  while(*temp++ != 0);

  for(temp = max; (*temp & ~32); ) 
  {
    printf("%c", *temp++);
  }
}

char seperate(msg)
{

  char *temp = msg;
  int i;

  printf("Alinur Caglayan - 18050127622 - Lab Work 5 ");
  printf("Please enter a string message with maximum of 40 characters: ");
  gets(msg);

  for(i=0; i < 41; i++)
  {
    if(*temp & ~32)
    {
      printf("\n");
    }
    else
    {
      printf("%c", temp);
    }
    system("pause");
  }
}

3 个答案:

答案 0 :(得分:1)

您在没有首先声明其类型的情况下调用您的函数。在C标准的两个最新版本中不允许这样做,并且应该产生诊断消息。

但是,如果您的编译器符合较旧的标准(或根本不符合),则调用未声明的函数将导致编译器提供其自己的声明,其返回类型默认为int。当您稍后将函数定义为具有不同的返回类型时,编译器会警告您不匹配。

在调用它们之前,你应该总是声明你的函数(即返回类型及其参数的类型),,定义你的函数(即函数体)。

答案 1 :(得分:1)

有很多错误。

您应该在main之前声明您的函数,如下所示:

char shortest(char msg[41]);
char longest(char msg[41]);

或者如果您不想声明它们,您可以在main ...

之前定义它们

你也有:

scanf("%d", selector);

虽然它应该是:

scanf("%d", &selector);

你的所有功能都应该返回一个字符。

已编辑:另一件事是您定义了以下功能:

char longest(msg) {
    ...
}

但你必须指定参数的类型,如下所示。

char longest(char msg[41]) {
    ...
}

答案 2 :(得分:1)

以下仅更正基本语法,而不查看子函数实际执行的操作:

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

// prototypes
void longest ( char * );
void shortest( char * );
void seperate( char * );

int main(void)
{
    char msg[41];
    int selector = 0;

    printf("Alinur Caglayan - 18050127622 - Lab Work 5 ");
    printf("Please enter a string message with maximum of 40 characters: ");
    if( NULL == fgets(msg, sizeof(msg), stdin) )
    {
        perror( "fgets failed" );
        exit( EXIT_FAILURE );
    }

    int done = 0; // indicate not done

    while(!done)
    { 
        printf("Please select one of the following functions:\n");
        printf( "1) Longest Word Function\n");
        printf( "2) Shortest Word Function\n");
        printf( "3) Separate Words Function\n");
        printf( "4) Exit:\n");

        if( 1 != scanf(" %d", &selector) )
        {
            perror( "scanf failed" );
            exit( EXIT_FAILURE );
        } 

        switch( selector )
        {
            case 1:
                longest(msg);
                break;

            case 2:
                shortest(msg);
                break;

            case 3:
                seperate(msg);
                break;

            case 4:
                done = 1; // cause loop to exit
                break;

            default:
                printf("\nERROR: invalid selection entered\n");
                break;
        } // end switch
    } // end while

    return(0);
} // end funtion: main


void longest( char* msg)
{
    char *temp = msg, *word = msg, *max = msg;
    int increment = 0, incrementmax = 0;

    do {
        if(!(*temp & ~32)) 
        {
            if(increment > incrementmax) 
            {
                incrementmax = increment;
                max = word;
            } // end if

            increment = 0;
        }

        else if(!increment++) 
        {
            word = temp;
        } // end if
    } while(*temp++ != 0);

    for(temp = max; (*temp & ~32); ) 
    {
    printf("%c", *temp++);
    }
} // end function: longest

void shortest(char* msg)
{
    char *temp = msg, *word = msg, *max = msg;
    int increment = 0, incrementmax = 0;

    do {

        if(!(*temp & ~32)) 
        {

            if(increment > incrementmax) 
            {
                incrementmax = increment;
                max = word;
            } // end if
            increment = 0;
        }

        else if(!increment++)
        {
            word = temp; 
        }
    } while(*temp++ != 0);

    for(temp = max; (*temp & ~32); temp++) 
    {
        printf("%c", *temp);
    }
} // end function: shortest

void seperate(char* msg)
{

    char *temp = msg;
    int i;

    printf("Alinur Caglayan - 18050127622 - Lab Work 5 ");
    printf("Please enter a string message with maximum of 40 characters: ");
    if( NULL == fgets(msg, sizeof(msg), stdin) )
    {
        perror( "fgets failed" );
        exit( EXIT_FAILURE );
    }

    for(i=0; i < 41; i++)
    {
        if(*temp & ~32)
        {
            printf("\n");
        }
        else
        {
            printf("%c", temp);
        }  // end if

        system("pause");
    } // end for
} // end function: seperate
相关问题