【问题标题】:i'm unable to compile a simple C function program in Dev c++?我无法在 Dev c++ 中编译一个简单的 C 函数程序?
【发布时间】:2016-06-22 00:52:02
【问题描述】:

我正在尝试在 Dev-C++ 中编译, 程序是:

main( )
{
    message( ) ;
    printf ( "\nCry, and you stop the monotony!" ) ;
}

message( )
{
    printf ( "\nSmile, and the world smiles with you..." ) ;
}

错误是:

E:\Dev\fn.cpp [Error] 'message' was not declared in this scope
E:\Dev\fn.cpp [Error] ISO C++ forbids declaration of 'message' with no type [-fpermissive]

编辑:愚蠢的错误。正在将c 程序编译为cpp

【问题讨论】:

  • 你是用c写代码并编译成c++吗?
  • 这不是有效的 C,也不是 C++,所以难怪。我认为这本书因教授陈旧过时的东西而声名狼藉。
  • 如果是C书,你应该尝试用C编译器编译。此外,这本书看起来可能是 C89 之前的标准。许多 C 编译器会拒绝该代码。
  • 请记住,C 和 C++ 是两种完全不同的语言,而且您使用的书是负值的,所以扔掉它就是利润。
  • @juanchopanza 这是标准 C89(如果包含适当的标头)。请问哪个编译器讨厌那个代码?我一个都不认识。

标签: c dev-c++


【解决方案1】:

首先,将 C 代码编译为 C 代码,而不是 C++ 代码。 C代码的扩展名应该是.c,而不是.cpp

然后,认识到书中可能有错误的东西和正确的代码。 不应省略返回值和参数的类型,使用的函数应在使用前声明。

试试这个:

#include <stdio.h> /* declaration of printf() will be here */

/* bring this function before using */
void message(void)
{
    printf ( "\nSmile, and the world smiles with you..." ) ;
}

int main(void)
{
    message( ) ;
    printf ( "\nCry, and you stop the monotony!" ) ;
    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多