原型bool函数,undefined Foo

时间:2011-11-19 11:56:37

标签: c

我的代码很少:

// prototype
bool Foo();

int main( ... )
{
  if( Foo() == false )
    return 1;

  return 0;
}

bool Foo()
{
  return true;
}

这有什么问题?编译器(VS 2010)显示了很多语法错误,“)”和“;”并且我的原型没有做到这一点?

3 个答案:

答案 0 :(得分:1)

我认为你的问题是没有定义bool。

这取决于你的编译器是如何设置的,但是bool是一种在没有自己定义或包含它的情况下经常不受支持的类型。

我发现了这个:

http://msdn.microsoft.com/en-us/library/tf4dy80a.aspx

干杯

杰森

答案 1 :(得分:0)

我不熟悉那个特定的编译器,但从错误的位置判断它可能需要void参数列表:

bool Foo(void);

......没有争论。 (我知道C89允许一个空的参数列表用于未指定的参数,但是VS2010可能不遵循这一点。我知道我已经使用过没有的专有编译器。)

答案 2 :(得分:0)

我也有这个问题。关键是第一个错误:

Missing type specifier; assuming 'int'.

这意味着它无法找到类型说明符。它是由bool未定义为数据类型引起的,而编译器假设您正在创建一个名为bool的函数。

bool      //What the program says
int bool  //What it thinks it means

然后在看到Foo时会感到困惑,几乎所有事情都会发生。

Syntax error: expected ';' but found 'Foo'.
Missing type specifier; assuming 'int'.
Undeclared function 'Foo' (did you mean '(no name)'?); assuming 'extern' returning 'int'.
Syntax error: expected ';' but found 'Foo'.
Missing type specifier; assuming 'int'.
Undeclared identifier 'true' (did you mean 'main'?).

解决方案:定义bool。最简单的方法是通过将这行代码放在程序的顶部来包含标准的布尔标题:

#include <stdbool.h>

这样做的另一个好处是,您可以访问多个标准函数来处理布尔值,以及truefalse标识符。