黑马程序员技术交流社区
标题:
看OC视频,遇到问题
[打印本页]
作者:
tianlin
时间:
2015-3-16 13:28
标题:
看OC视频,遇到问题
/* 学生
*/
#import <Foundation/Foundation.h>
typedef enum
{
SexMan,
SexWoman
}Sex;
typedef struct
{
int year;
int mouth;
int day;
}Date;
typedef enum
{
ColorBlack,
ColorRed,
ColorGreen
}Color;
@interface Dog : NSObject
{
@public
double weight;
Color curColor;
}
-(void)eat;
-(void)run;
@end
@implementation Dog
- (void) eat
{
weight+=1;
NSLog(@"狗吃完这次后体重是%f",weight);
}
- (void) run
{
weight-=1;
NSLog(@"狗跑完这次体重是%f",weight);
}
@end
@interface Student : NSObject
{
@public
Sex sex;
Date birthday;
double weight;
Color favColor;
char *name;
Dog *dog;
}
- (void)eat;
- (void)run;
- (void)print;
- (void)liuDog;
- (void)weiDog;
@end
@implementation Student
- (void) eat
{
weight+=1;
NSLog(@"吃完这次后体重是%f",weight);
}
- (void) run
{
weight-=1;
NSLog(@"跑完这次体重是%f",weight);
}
-(void) print
{
NSLog(@"性别=%d, 颜色=%d, 生日=%d-%d-%d 姓名%s",sex,favColor,birthday.year,birthday.mouth,birthday.day,name);
}
- (void)liuDog
{
[dog run];
}
- (void)weiDog
{
[dog eat];
}
@end
int main()
{
Student *s = [Student new];
//Dog *d = [Dog new]; 这是视频中的写法,给类中的类赋值,必须用这种方式??
//d->curColor = ColorGreen;
//d->weight = 20;
//s->dog = d;
s->dog->weight = 100;
s->dog->curColor = ColorGreen;
[s liuDog];
[s weiDog];
return 0;
}
复制代码
编译连接没有错误,但是执行的时候出现 Segmentation fault: 11的提示 什么意思
作者:
tianlin
时间:
2015-3-17 16:23
没人回答呢
作者:
liboy
时间:
2015-3-17 16:36
//Dog *d = [Dog new]; 这是视频中的写法,给类中的类赋值,必须用这种方式??
//d->curColor = ColorGreen;
//d->weight = 20;
//s->dog = d;
s->dog->weight = 100;
s->dog->curColor = ColorGreen;
这段代码必须用你注释的一段代码,不能 s->dog->weight = 100;
s->dog->curColor = ColorGreen;这样写。。。。视频中老师专门强调过得。。。。
作者:
xiezhongmin
时间:
2015-3-17 16:46
应该是这//s->dog = d;这段不能注释掉的原因,
作者:
liboy
时间:
2015-3-17 16:46
还有- (void)liuDog:(Dog *)d
{
[d run];
}
- (void)weiDog:(Dog *)d
{
[d eat];
}这两个方法要加参数,没有参数怎么知道,是哪只狗呢??
作者:
勇闯☀☀黑马
时间:
2015-3-17 17:07
好深奥啊
作者:
liboy
时间:
2015-3-17 17:18
liboy 发表于 2015-3-17 16:46
还有- (void)liuDogDog *)d
{
[d run];
这刚才说错了我,搞错了不需要改,不好意思,呵呵:#
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2