xcode 没有以前的原型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9542031/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:30:46  来源:igfitidea点击:

No previous prototype?

cxcodefunction

提问by Stas Jaro

Possible Duplicate:
Error: No previous prototype for function. Why am I getting this error?

可能的重复:
错误:没有以前的函数原型。为什么我收到这个错误?

I have a function that I prototyped in the header file, however Xcode still gives me warning No previous prototype for the function 'printBind'. I have the function setBindprototyped in the same way but I do not get an warning for this function in my implementation.

我有一个在头文件中原型化的函数,但是 Xcode 仍然给我警告No previous prototype for the function 'printBind'。我setBind以相同的方式对函数进行了原型设计,但在我的实现中没有收到有关此函数的警告。

CelGL.h

细胞生长素

#ifndef Under_Siege_CelGL_h
#define Under_Siege_CelGL_h

void setBind(int input);
void printBind();

#endif

CelGL.c

CelGL.c

#include <stdio.h>
#include "CelGL.h"

int bind;

void setBind(int bindin) { // No warning here?
    bind = bindin;
}

void printBind() { // Warning here
    printf("%i", bind);
}

回答by Jonathan Leffler

In C, this:

在 C 中,这个:

void printBind();

is not a prototype. It declares a function that returns nothing (void) but takes an indeterminate list of arguments. (However, that list of arguments is not variable; all functions taking a variable length argument list musthave a full prototype in scope to avoid undefined behaviour.)

不是原型。它声明了void一个不返回任何内容 ( ) 但接受不确定参数列表的函数。(但是,该参数列表不是可变的;所有采用可变长度参数列表的函数都必须在范围内具有完整的原型,以避免未定义的行为。)

void printBind(void);

That's a prototype for the function that takes no arguments.

这是不带参数的函数的原型。

The rules in C++ are different - the first declares a function with no arguments and is equivalent to the second.

C++ 中的规则是不同的——第一个声明一个没有参数的函数,等价于第二个。

The reason for the difference is historical (read 'dates back to the mid-1980s'). When prototypes were introduced into C (some years after they were added to C++), there was an enormous legacy of code that declared functions with no argument list (because that wasn't an option before prototypes were added), so backwards compatibility considerations meant that SomeType *SomeFunction();had to continue meaning 'a function that returns a SomeType *but for which we know nothing about the argument list'. C++ eventually added the SomeType *SomeFunction(void);notation for compatibility with C, but didn't need it since type-safe linkage was added early and all functions needed a prototype in scope before they were defined or used.

差异的原因是历史性的(读作“可追溯到 1980 年代中期”)。当原型被引入 C 时(在它们被添加到 C++ 几年后),有大量的代码遗留了声明没有参数列表的函数(因为在添加原型之前这不是一个选项),所以向后兼容性考虑意味着那SomeType *SomeFunction();必须继续意味着“一个返回 aSomeType *但我们对参数列表一无所知的函数”。C++ 最终添加了SomeType *SomeFunction(void);与 C 兼容的符号,但并不需要它,因为早期添加了类型安全链接,并且所有函数在定义或使用之前都需要范围内的原型。