本帖最后由 执着的土豆 于 2014-4-25 14:28 编辑
- <div class="blockcode"><blockquote>#import <Foundation/Foundation.h>
- @interface Dog:NSObject
- {
- @public
- int DogWeight;
- }
- -(void) DogEat;
- -(void)DogMessage;
- @end
- @implementation Dog
- -(void)DogEat
- {
- DogWeight+=1;
- NSLog(@"狗吃完这顿的体重是%d",DogWeight);
- }
- <font color="#ff0000">-(void)DogMessage</font>
- {
- NSLog(@"狗的名字是:旺财,狗的体重是%d",DogWeight);
- }
- @end
- @interface Student:NSObject
- {
- @public
- Dog *dog;
- }
- -(void)weigou;
- @end
- @implementation Student
- -(void)weigou
- {
- [dog DogEat];
- }
- @end
- int main()
- {
- Student *S=[Student new];
- Dog *D=[Dog new];
- S->dog=D;
- //[S->dog DogEat] <font color="#ff0000">//问题1:这样写不报错,但是也不会输出什么,这是为嘛?</font>
- [S weigou];
- <font color="#ff0000"> //问题2:在不增加student里面的方法的前提下,能不能调用实例S所指向的DogMessage方法,输出狗的信息?</font>
- return 0;
- }
复制代码
问题3: 这个程序敲完,运行,改掉错误之后,编译器会出现以下错误,何解?
MacdeMacBook-Pro:Test mac$ cc 10-OC习题1.m
Undefined symbols for architecture x86_64:
"_NSLog", referenced from:
_main in 10-OC习题1-f1cb02.o
"_OBJC_CLASS_$_NSObject", referenced from:
_OBJC_CLASS_$_Car in 10-OC习题1-f1cb02.o
"_OBJC_METACLASS_$_NSObject", referenced from:
_OBJC_METACLASS_$_Car in 10-OC习题1-f1cb02.o
"___CFConstantStringClassReference", referenced from:
CFString in 10-OC习题1-f1cb02.o
"__objc_empty_cache", referenced from:
_OBJC_METACLASS_$_Car in 10-OC习题1-f1cb02.o
_OBJC_CLASS_$_Car in 10-OC习题1-f1cb02.o
"_objc_msgSend", referenced from:
_main in 10-OC习题1-f1cb02.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation) 还希望各位高手给予解惑,感激不尽。
|