没有函数BOOL的原型isIntsDifferent(int thing1,int thing2)和NSString * boolString(BOOL yesNo)

时间:2012-04-02 08:18:09

标签: objective-c

我从一本书中获取此代码,但我无法使其工作。我的问题在这里是什么?正如标题所说

“之前没有函数BOOL的原型isIntsDifferent(int thing1,int thing2)和NSString * boolString(BOOL yesNo)”

#import <Foundation/Foundation.h>

BOOL areIntsDifferent (int thing1, int thing2 ) {
    if ( thing1 == thing2) {
        return (NO);
    }
    else {
        return (YES);
    }
}    

NSString * boolString (BOOL yesNo) {
    if (yesNo== NO) {
        return (@"NO");
    }
    else {
        return (@"YES");
    }    
} 

int main (int argc, const char * argv[]) {
    BOOL aretheydiffrent;
    aretheydiffrent = areIntsDifferent (5,5);
    NSLog(@"are %d and %d diffrent? %@", 5, 5, boolString(aretheydiffrent));
    aretheydiffrent = areIntsDifferent(23,42);
    NSLog(@"are %d and %d diffrent? %@", 23, 42, boolString(aretheydiffrent));
    return 0;
}

3 个答案:

答案 0 :(得分:3)

错误消息转换为:

  

这意味着GCC找不到全局函数定义   该功能的原型。如果函数用于多个函数   文件,在某个地方的头文件中应该有一个原型。   这可以防止功能及其用途不同步

使函数static应该对此进行排序:

#import < Foundation/Foundation.h >

static BOOL areIntsDifferent (int thing1, int thing2 )
{
     return thing1 != thing2;
}

static NSString * boolString (BOOL yesNo)
{
    return yesNo ? @"YES" : @"NO";

}

int main (int argc, const char * argv[])

{
   BOOL aretheydiffrent;

   aretheydiffrent = areIntsDifferent (5,5);

   NSLog(@"are %d and %d diffrent? %@",
      5, 5, boolString(aretheydiffrent));

   aretheydiffrent = areIntsDifferent(23,42);

   NSLog(@"are %d and %d diffrent? %@",
      23, 42, boolString(aretheydiffrent));

   return 0;
}

答案 1 :(得分:1)

首先我们不应该使用静态,其次我们不应该在目标c(oc)中使用'==',当我们使用bool时,因为在oc中,数字是用 像0000 0000那样的内部存储,所以'5'读为000000 00021,'23'读为'0001 0111''42' 在oc中,计算机可以认为23为5,这是错误的。 出于同样的原因,我们不应该使用== yes

以下是一个例子:

NSLog(@"%d  %d",YES,NO);
if(areIntsDifferent_faulty(23, 5)==YES)
{
   NSLog(@"Are %d and %d different? %d YES",23,5,areIntsDifferent_faulty(23, 5));
}
else {
    NSLog(@"Are %d and %d different? %d NO",23,5,areIntsDifferent_faulty(23, 5));
}

if(areIntsDifferent_faulty(23, 5))
{
    NSLog(@"Are %d and %d different? %d YES",23,5,areIntsDifferent_faulty(23, 5));
}
else {
    NSLog(@"Are %d and %d different? %d NO",23,5,areIntsDifferent_faulty(23, 5));
}

输出: 是5和5不同?没有 23和42不同吗?是的 1 0 是23和5不同? 18没有 是23和5不同? 18是的

所以这样写:if(areIntsDiffreent_faulty(23,5))这是对的!

答案 2 :(得分:0)

函数原型本质上是函数的“签名”:它的名称是什么,它返回什么,它的参数是什么等等。函数体是函数实际做的,是括号之间的内容。功能。

<强>原型:

BOOL areIntsDifferent(int thing1, int thing2);

<强>体:

BOOL areIntsDifferent(int thing1, int thing2)
{
    return (thing1 != thing2);
}

您很可能只收到编辑警告,而不是错误(警告只是告诉您一些事情,您的程序可能仍然有效,错误实际上会停止显示)。函数原型在大型项目中非常有用,通常包含在一个名为header的单独文件中。我不知道你有哪本书,但你可能会在以后详细了解这本书。

有两种可能的解决方法:

  • #import ...

    之后,将以下行添加到文件顶部

    BOOL areIntsDifferent(int thing1, int thing2);
    NSString* boolString(BOOL yesNo);

  • 在Xcode项目配置设置中关闭“缺少函数原型”警告。

第一个修复实际上是将函数原型添加到文件顶部,以满足警告。第二个修复实际上是禁用警告,因此编译器不会启动它。

第一个问题就像是“我听到了你,感谢提醒我,Compiler先生。”第二个修复就像是“关闭编译器,我是一个大男孩,并且我的功能正好在我想要的地方!”