![](/img/trans.png)
[英]error C2371: 'functionname' redefinition: different basic types
[英]error C2371, redefinition; different basic types
對於函數,我得到錯誤代碼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");
}
}
您在調用函數時沒有先聲明它們的類型。 這兩個最新版本的C標准不允許這樣做,並且應導致產生診斷消息。
但是,如果您的編譯器符合較舊的標准(或完全沒有),則調用未聲明的函數將導致編譯器提供其自己的聲明,其返回類型默認為int
。 以后將函數定義為具有不同的返回類型時,編譯器會警告您不匹配。
您應該始終聲明函數(即返回類型及其參數的類型), 或者在瑣碎的情況下,在調用函數之前先定義函數(即具有函數體)。
有很多錯誤。
您應該在main之前聲明函數,如下所示:
char shortest(char msg[41]);
char longest(char msg[41]);
或者如果您不想聲明它們,可以在main之前定義它們。
您也有:
scanf("%d", selector);
雖然應該是:
scanf("%d", &selector);
並且您的所有functioncs也應該返回一個char。
編輯:另一件事是您定義函數,例如:
char longest(msg) {
...
}
但是您必須像這樣指定參數的類型。
char longest(char msg[41]) {
...
}
以下內容僅糾正了基本語法,而沒有研究子功能的實際作用:
#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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.