0
我附上以下程序代碼以顯示分數的實際值,並且構建成功,但程序崩潰且線程錯誤。你能告訴我,Iam在哪裏犯了一個錯誤。真的很感謝幫助。
感謝
Maverick10線程錯誤 - 線程1:Breakpoint1.1
#import<Foundation/Foundation.h>
@interface Fraction : NSObject
{
int numerator;
int denominator; // variables to be used
}
-(void) print;
-(void) setNumerator:(int) n;
-(void) setDenominator:(int) d;
-(int) numerator;
-(int) denominator;
-(double) convertToNum; // new method declared
@end
@implementation Fraction
-(void) print
{
NSLog(@"%i/%i",numerator,denominator);
}
-(void) setNumerator :(int) n
{
numerator=n;
}
-(void) setDenominator :(int)d
{
denominator=d;
}
-(int)numerator
{
return numerator;
}
-(int) denominator
{
return denominator;
}
-(double) convertToNum
{
if(denominator!=0) // checking for denominator to be non zero
return (double) numerator/denominator;
else
return NAN;
}
@end
//Program Section
int main(int argc, char*argv[])
{
NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc] init];
Fraction * aFraction =[[Fraction alloc] init];
Fraction * bFraction =[[Fraction alloc] init];
[aFraction setNumerator:1]; // setting the numerator for the first fraction.Similarly denominator
[aFraction setDenominator:4];
[aFraction print];
NSLog(@"= ");
NSLog(@"%g",[aFraction convertToNum]);
[bFraction print];
NSLog(@"=");
NSLog(@"%g",[bFraction convertToNum]);
[aFraction release];
[bFraction release];
[pool drain];
return 0;
}
我剛剛在Xcode中運行你的程序,它工作正常。你得到什麼線程錯誤? – yeesterbunny
我得到這個錯誤 - 線程1:Breakpoint1.1在我定義的行 - (void)setNumerator:(int)n - 方法。我得到的輸出是lldb。 – Maverick10
我懷疑你不小心在那裏添加了一個斷點。如果你看 - (void)setNumerator的左邊,你看到一個藍色的矩形箭頭指向它嗎? – yeesterbunny