C语言 错误 C2371:重新定义;不同的基本类型 - 为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16424239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error C2371: redefinition; different basic types - why?
提问by Billie
I have the following code:
我有以下代码:
#include <stdio.h>
#include <stdlib.h>
// helping
void sortint(int numbers[], int array_size)
{
int i, j, temp;
for (i = (array_size - 1); i > 0; i--)
{
for (j = 1; j <= i; j++)
{
if (numbers[j-1] > numbers[j])
{
temp = numbers[j-1];
numbers[j-1] = numbers[j];
numbers[j] = temp;
}
}
}
}
// exer1 - A
void sort(int** arr, int arrsize) {
int i = 0;
// sort....
for(; i < arrsize; ++i) {
sortint((*(arr+i))+1, **(arr+i));
}
}
// Exer1 - B
void print(int** arr, int arrsize) {
int i = 0, j, size, *xArr;
for(; i < arrsize; ++i) {
size = **(arr+i);
xArr = *(arr+i);
printf("size: %d: ", size);
// print elements
for(j = 1; j <= size; ++j) printf("[%d], ", *(xArr+j));
printf("\n");
}
}
// Exer2:
void exera() {
int* ptr = (int*)malloc(sizeof(int));
if(!ptr) exit(-1);
eb(ptr);
free(ptr);
}
void eb(int* ptr) {
int* arr = (int*) malloc(sizeof(int) * (*ptr));
int i = 0;
for(; i < *ptr; ++i) scanf("%d", arr+i);
ec(arr, *ptr);
}
void ec(int* arr, int size) {
int i;
sortint(arr, size);
for(i = 0; i < size; ++i) printf("[%d], ", *(arr+i));
}
int main() {
// Exer1:
int a[] = {4,3,9,6,7};
int b[] = {3,2,5,5};
int c[] = {1,0};
int d[] = {2,1,6};
int e[] = {5,4,5,6,2,1};
int* list[5] = {a,b,c,d,e};
sort(list, 5); // A
print(list, 5); // B
printf("\n\n\n\n\n");
// Exer2:
exera();
fflush(stdin);
getchar();
return 0;
}
I get these errors:
我收到这些错误:
Error 2 error C2371: 'eb' : redefinition; different basic types
source.c 56
Error 4 error C2371: 'ec' : redefinition; different basic types
source.c 63
Warning 1 warning C4013: 'eb' undefined; assuming extern returning int
source.c 52
Warning 3 warning C4013: 'ec' undefined; assuming extern returning int
source.c 60
I tried to change function names - for nothing.
我试图更改函数名称 - 一无所获。
Why is that error is being shown? I'm using Visual C++ Express 2010.
为什么会显示该错误?我正在使用 Visual C++ Express 2010。
回答by Shafik Yaghmour
You are trying to call eband ecbefore they are declared or defined. Move the definition of ecbefore eband both before exera. You could also forward declare your functions before you define any of them like so:
您正在尝试在声明或定义它们之前调用eband ec。移动ecbeforeeb和 both before的定义exera。您还可以在定义任何函数之前转发声明您的函数,如下所示:
void eb(int* ptr) ;
void ec(int* arr, int size) ;
回答by simonc
You call ebfrom exera, before ebis declared. The compiler assumes it'll return intthen finds an implementation that returns voidfurther down the file.
在声明之前,您eb从exera,调用eb。编译器假定它会返回,int然后找到一个返回void文件更远的实现。
The most common fix is to declare your local functions at near top of your file
最常见的修复方法是在文件顶部附近声明本地函数
void eb(int* ptr);
// repeat for each other function which generates the same error
回答by K Scott Piel
If you are going to place the function ebafter the point at which it is called, then you need to place a prototype for it before it is called... otherwise, C will use the default prototype and then your function ends up redefining it, thus the error you received.
如果您打算将函数放置在调用eb它的点之后,那么您需要在调用之前为其放置一个原型……否则,C 将使用默认原型,然后您的函数最终会重新定义它,因此你收到的错误。
Alternatively, you can move the functions themselves before they are used in the source file, but that's not always possible. Placing prototypes at the top of the file or, better still, in a header file you can include anywhere you will use the functions is the best alternative.
或者,您可以在源文件中使用函数之前移动函数本身,但这并不总是可行的。将原型放在文件的顶部,或者更好的是,放在头文件中,您可以将其包含在您将使用这些函数的任何地方,这是最好的选择。

