C 错误:原型函数声明不在范围内

发布于 2024-11-18 05:26:50 字数 303 浏览 1 评论 0原文

我不明白我的错误是什么:

 void main(char string[])
 {
   strcpy(command_E,string);
   processCMD(); /*FIRST ERROR:  prototype function declaration not in scope */
 }

 void processCMD(void) /*SECOND ERROR: external item attribute mismatch */
 {
  .... /*rest of code not displayed*/

I don't understand what my error is:

 void main(char string[])
 {
   strcpy(command_E,string);
   processCMD(); /*FIRST ERROR:  prototype function declaration not in scope */
 }

 void processCMD(void) /*SECOND ERROR: external item attribute mismatch */
 {
  .... /*rest of code not displayed*/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

饮惑 2024-11-25 05:26:50

在您使用 processCMD() 时,您还没有为其声明原型,因此它会获得一个默认原型。

您尚未声明它的事实导致了第一个错误。

事实上,您的实际定义与创建的默认定义相冲突,因为您没有声明它,这是第二个错误的原因。

解决方案是要么在使用前定义函数:

void processCMD(void) {
    blah blah blah
}

void main (char string[]) {  // not really a good definition for main, by the way.
    strcpy(command_E,string);
    processCMD();
}

要么在使用前提供原型:

void processCMD(void);

void main (char string[]) {  // not really a good definition for main, by the way.
    strcpy(command_E,string);
    processCMD();
}

void processCMD(void) {
    blah blah blah
}

对于 main 的声明,两种规范形式是:

int main (void);
int main (int argc, char *argv[]); // or char **argv.

标准允许的其他形式(实现定义的),但那些两个是必需的(至少对于托管实现而言 - 像嵌入式系统或操作系统这样的独立实现可以很好地完成他们想做的任何事情)。

At the point where you use processCMD(), you haven't declared a prototype for it, so it gets a default one.

The fact that you haven't declared it causes the first error.

The fact that your actual definition conflicts with the default one created because you hadn't declared it is the cause of your second error.

The solution is to either define the function before use:

void processCMD(void) {
    blah blah blah
}

void main (char string[]) {  // not really a good definition for main, by the way.
    strcpy(command_E,string);
    processCMD();
}

or provide a prototype before use:

void processCMD(void);

void main (char string[]) {  // not really a good definition for main, by the way.
    strcpy(command_E,string);
    processCMD();
}

void processCMD(void) {
    blah blah blah
}

As to the declaration of main, the two canonical forms are:

int main (void);
int main (int argc, char *argv[]); // or char **argv.

Others are allowed by the standard (implementation defined) but those two are required (at least for hosted implementations - freestanding implementations like embedded systems or operating systems can pretty well do whatever they want).

破晓 2024-11-25 05:26:50

在第一次使用该函数之前,您需要一个原型来声明它,以便编译器知道该函数采用哪种参数(如果有)以及它返回什么:

void processCMD(void); // prototype

void main(char string[])
 {
   strcpy(command_E,string);
   processCMD();
 }

 void processCMD(void)
 {
  .... /*rest of code not displayes*/

或者,您可以在第一次使用该函数之前定义该函数(因为定义 通常,原型将

放入头文件中,以便其他模块可以使用该函数,因为函数定义只能在一个源文件中(除非将函数设置为静态,这会使每个实例)函数“私有”到模块,或者标记 ,可以实现多个定义)

函数作为内联

  • 。您传递了正确的参数,它将对参数应用“默认促销”(基本上将内容转换为 intdouble 适当的),并假设该函数返回一个int。如果这些假设是错误的,那么程序就是不正确的并且将出现未定义的行为。编译器通常可以配置为在调用没有原型的函数时发出警告或错误(显然你的函数有原型)。在 C++ 中,在没有看到先前原型(或完整函数定义)的情况下调用函数始终是错误。

  • 在 C 语言中,有以下区别:

    void processCMD();
    

    void processCMD(void);
    

    第一个是函数声明,但不是原型 - 它告诉编译器 processCMD 是一个函数,并且它返回 void 而不是 int< /代码>。但它不会告诉编译器有关参数的任何信息。编译器仍然允许使用传递的参数进行函数调用,并将应用默认的提升。 (同样,此功能可能会被编译器配置禁用)。

    第二个是一个原型,专门告诉编译器该函数不带参数。

    在 C++ 中,两者是等效的,并且明确告诉编译器该函数不带参数。

You need a prototype before the the first use of the function to declare it so the compiler knows what kind of arguments the function takes (if any) and what it returns:

void processCMD(void); // prototype

void main(char string[])
 {
   strcpy(command_E,string);
   processCMD();
 }

 void processCMD(void)
 {
  .... /*rest of code not displayes*/

Alternatively, you could defined the function before it's first use (since the definition will provide the information the compiler wants.

Typically, the prototype will go in a header file so other modules can use the function since function definitions can only be in one source file (barring such things as making the function static, which makes each instance of the function 'private' to a module, or marking the function as inline, which enables multiple definitions).

Some additional prototype trivia:

  • Generally in C, the lack of a protoype is not an error in itself - if the compiler sees a function call without having seen a prototype it will assume that you're passing the right parameters, it will apply 'default promotions' to paramters (basically convert things to int ot double as appropriate), and will assume the function returns an int. If those assumptions are wrong, then the program is incorrect and will have undefined behavior. Compilers can often be configured to emit warnings or errors for calling functions that don't have prototypes (apparently yours has). In C++, calling a function without a previous protoype being seen (or the full function definition) is always an error.

  • In C, there's a difference between:

    void processCMD();
    

    and

    void processCMD(void);
    

    The first is a function declaration, but not a prototype - it tells the compiler that processCMD is a function and that it returns void instead of int. But it doesn't tell the compiler anything about the arguments. The compiler will still allow function calls to be made with arguments passed and will apply the default promotions. (Again, this feature might be disabled by compiler configuration).

    The second is a prototype that specifically tells the compiler that the function takes no arguments.

    In C++, both are equivalent and specifically tell the compiler that the function takes no arguments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文