objective-c - 初始化错误中的类型不兼容

标签 objective-c xcode compiler-errors

我已经花了几个小时了,找不到解决方案。当我尝试编译以下内容时,在标记的行上出现“初始化时不兼容的类型”错误。任何帮助都可以接受。

这是我的Controller.m文件:

#import "Controller.h"
#import "Transactions.h"

@implementation Controller
- (IBAction)add:(id)sender {
    double x = [addF doubleValue];
    double bal = [balanceF doubleValue];
    double newBal = [trans add:x to:bal]; //Error marked here
    [balanceF setDoubleValue:newBal];
}

- (IBAction)sub:(id)sender {
    double x = [subF doubleValue];
    double bal = [balanceF doubleValue];
    double newBal = [trans sub:x from:bal]; //Error marked here
    [balanceF setDoubleValue:newBal];
}
@end

这是我的Transactions.m文件:
#import "Transactions.h"

@implementation trans

-(double) add:(double)x to:(double)bal{
    return (x + bal);
}

-(double) sub:(double)x from:(double)bal{
    return (bal - x);
}

@end

请告诉我您是否还有其他需要。
谢谢!

最佳答案

'trans'是一个类,因此,如果您使用该类声明/调用方法,而不是其实例,则应使用+进行声明:

+(double) add:(double)x to:(double)bal{

+(double) sub:(double)x from:(double)bal{

别忘了更改头文件...

关于objective-c - 初始化错误中的类型不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6683558/

相关文章:

android-studio - flutter/Dart 错误 : The argument type 'Null Function(Key)' can't be assigned to the parameter type 'void Function(Object)'

process - Contiki编译错误, “ERROR: address 0x820003 out of range at line 1740 of…”

objective-c - Uiswitch开/关

objective-c - 如何在Cocoa中绘制锥形线+椭圆形阴影

ios - 在 Xcode 中使用 -force_load 链接器标志会导致重复符号错误

ios - 非公开 API 使用 : Apps are not permitted to access the UDID and must not use the uniqueIdentifier method of UIDevice

ios - UICollectionView 中带有 endItemAnimations 的断言失败

objective-c - 如何为原点设置动画?

xcode - 在 OSX 上安装 GLFW

c - “noreturn”函数确实返回