隐式声明,而实现可在 xcode 中使用
当我使用 xcode 编译这两个源代码时,我收到警告和错误:
- 函数“shouldHavePlayed”的隐式声明。 (在带有注释的行)
- 链接时出错。
但是,如果您查看 .h 文件中定义的方法的代码,则包含 .h 文件,并且重新排列 .m 文件中的方法也无法解决问题。
这段代码有什么问题?
TimerItem.h:
#import <Foundation/Foundation.h>
@interface TimerItem : NSObject {
BOOL enabled;
NSTimeInterval startTime;
NSTimeInterval repeatTime;
int sound;
int played;
}
- (void) play;
- (void) resetPlays;
- (BOOL) ShouldPlay : (NSTimeInterval) interval;
- (int) shouldHavePlayed : (NSTimeInterval) interval;
@end
TimerItem.m:
#import "TimerItem.h"
@implementation TimerItem
-(BOOL) ShouldPlay : (NSTimeInterval) interval {
int should = shouldHavePlayed(interval);//
return (should > played);
}
-(void) play {
played++;
}
-(void) resetPlays {
played = 0;
}
-(int) shouldHavePlayed : (NSTimeInterval) interval
{
if (interval < startTime) {
return 0;
}
else {
if (repeatTime > 0.0) {
return (int) floor((interval-startTime)/repeatTime)+1;
}
else {
return 1;
}
}
}
@end
When I compile these two sources with xcode I get a warning and an error:
- Implicit declaration of function 'shouldHavePlayed'. (at the line with the comment)
- an error while linking.
But if you look at the code the method is defined in the .h file, the .h file is included and rearranging the methods in the .m file doesn't solve the problem either.
What is wrong with this code?
TimerItem.h:
#import <Foundation/Foundation.h>
@interface TimerItem : NSObject {
BOOL enabled;
NSTimeInterval startTime;
NSTimeInterval repeatTime;
int sound;
int played;
}
- (void) play;
- (void) resetPlays;
- (BOOL) ShouldPlay : (NSTimeInterval) interval;
- (int) shouldHavePlayed : (NSTimeInterval) interval;
@end
TimerItem.m:
#import "TimerItem.h"
@implementation TimerItem
-(BOOL) ShouldPlay : (NSTimeInterval) interval {
int should = shouldHavePlayed(interval);//
return (should > played);
}
-(void) play {
played++;
}
-(void) resetPlays {
played = 0;
}
-(int) shouldHavePlayed : (NSTimeInterval) interval
{
if (interval < startTime) {
return 0;
}
else {
if (repeatTime > 0.0) {
return (int) floor((interval-startTime)/repeatTime)+1;
}
else {
return 1;
}
}
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你像 C 函数一样调用它,而不是 objc 消息。尝试这样做:
you are calling it like a C function, not an objc message. try it this way instead: