简体   繁体   English

C中的冲突类型错误与某些文件io

[英]Conflicting Types error in C with some file io

So here is my code 所以这是我的代码

int main(int argc, char *argv[] )
{
    double mass1, mass2, tof, pixcm;
    char pofVfilename[50];
    double pix[50];
    double pofV[50];
    if(argc != 2)
    {
        printf("usage: %s filename", argv[0]); 
   }
else
    {
        FILE *file = fopen(argv[1], "r");
        if(file == 0){
            printf("could not open file\n");
        }
        else{
            fscanf(file, "%lf %lf", &mass1, &mass2);
            fscanf(file, "%lf", &tof);
            fscanf(file, "%s", pofVfilename);
            fscanf(file, "%lf", &pixcm);
            fclose(file);
            printf("%lf%lf%lf%lf", mass1, mass2, tof, pixcm);  
            readinputpofV(pix, pofV, pofVfilename);
            printf("%f %f", pix[10], pofV[10]);
        }

    }
    return 0;
}


void readinputpofV(double pix[], double pofV[], char filname[]){
    FILE *file = fopen(filname, "r");
    if(file == 0){
        printf("could not open pofV file\n");
    }
    else{
        int result = 2;
        int i = 0;
        while(result == 2){
            result = fscanf(file, "%lf %lf", &pix[i], &pofV[i]);
            i++;
        }
    }
    fclose(file);
}

The error I get is 我得到的错误是

warning: conflicting types for 'readinputpofV' 警告:“ readinputpofV”的类型冲突

warning: previous implicit declaration of 'readinputpofV' was here 警告:先前隐含的'readinputpofV'声明就在这里

Can someone help. 有人可以帮忙吗 Also, I am brand new to file input and was hoping for some guidance on how I am doing. 另外,我是刚开始输入文件的人,希望能对我的工作提供一些指导。

This has nothing to do with the File I/O. 这与文件I / O 无关

Try putting void readinputpofV(double pix[], double pofV[], char filname[]); 尝试将void readinputpofV(double pix[], double pofV[], char filname[]); above main() . main() 之上

The call to the function is being taken as it's declaration, and the return type is being assumed as int . 对函数的调用被视为其声明,而返回类型被假定为int Hence the confusion. 因此混乱。

previous implicit declaration of 'readinputpofV' was here 先前隐含的'readinputpofV'声明就在这里

An implicit declaration makes the return type assumed to be int (in C89/C90, implicit declarations have been removed in C99). 隐式声明使返回类型假定为int (在C89 / C90中,隐式声明已在C99中删除)。 So the assumed type and the type of the definition are not compatible. 因此假定的类型和定义的类型不兼容。

Therefore the compiler warns about the conflicting types. 因此,编译器会警告有关类型冲突的信息。

It is just a warning, and not an error, though, and since the implemented type has void return type, you're not going to use the return value, so the code has a nonzero probability of working. 但是,这只是警告,而不是错误,并且由于实现的类型具有void返回类型,因此您将不使用返回值,因此代码的工作概率为非零。 But you should of course declare the function with the correct type before you are using it. 但是,在使用它之前,您当然应该使用正确的类型声明该函数。

You have to declare the readinputpofV() function before using it, else the compiler will assume it returns int , whereas it later encounters that it really returns void . 您必须在使用前声明readinputpofV()函数,否则编译器将假定它返回int ,而稍后遇到它实际上返回void

What you should do is adding 你应该做的是添加

void readinputpofV(double pix[], double pofV[], char filname[]);

before the definition of main() . main()的定义之前。

Since the compiler couldn't readinputpofV while is compiling main function is assuming that this function is returning an int If you move the definition of the function before the main. 由于编译器在编译主函数时无法读取inputpofV,因此假设您将函数的定义移至主函数之前,则此函数将返回int。 The warning will disappear. 警告将消失。

Besides declaring the function prototype above main, you can write the whole definition of readinputpofV above main. 除了在main之上声明函数原型之外,您还可以在main之上编写readinputpofV的整个定义。 Thus, you will avoid the error without needing to declare the function prototype. 因此,您无需声明函数原型就可以避免该错误。


I often use that tactic when I find it visually helpful, but it has it's downside: When you use multiple functions that interact with each other, that tactic may lead to compiler errors if you don't place the functions in the correct order. 当我发现它在视觉上有用时,我经常使用这种策略,但它有它的缺点:当你使用多个彼此交互的函数时,如果你没有按正确的顺序放置函数,那么这种策略可能会导致编译器错误。 C programs are compiled serially; C程序是按顺序编译的; invoked functions should be known. 应该知道调用的函数。

So, in C, to avoid implicit declaration compiler errors, any invoked function's definition or it's prototype, should be placed above any functions that might invoke it. 因此,在C中,为了避免隐式声明编译器错误,任何调用函数的定义或它的原型都应放在可能调用它的任何函数之上。 You can just write them from bottom to top. 您可以从下至上编写它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM