您的位置:首页 > 移动开发 > Objective-C

学习object-c时遇到的一个问题,报了一大堆的错,半天没头绪.原来是小问题,记载一下

2014-12-25 22:55 429 查看
@import <Foundation/Foundation.h>

@interface people : NSObject
{
    @public
    int age;
    char sex;
}

- (void) walk;

@end

@implementation people

- (void) walk
{
    NSLog(@"%c走了一段路,年纪%d",sex,age);
}

@end

void main()
{
    people *p1 = [people new];
    p1->age=20;
    p1->sex='F';
    
    
    people *p2 = [people new];
    p2->age=30;
    p2->sex='M';
    
    [p1 walk];
    
    [p2 walk];
    return;
    
}

在学习中的一段最简单的代码

报错如下

people.m:1:1: error: use of '@import' when modules are disabled
@import <Foundation/Foundation.h>
^
people.m:14:1: error: '@end' must appear in an Objective-C context
@end
^
people.m:17:17: warning: cannot find interface declaration for 'people'
@implementation people
                ^
people.m:21:5: warning: implicitly declaring library function 'NSLog' with type
      'void (id, ...)'
    NSLog(@"%c走了一段路,年纪%d",sex,age);
    ^
people.m:21:5: note: please include the header <Foundation/NSObjCRuntime.h> or
      explicitly provide a declaration for 'NSLog'
people.m:21:41: error: use of undeclared identifier 'sex'
    NSLog(@"%c走了一段路,年纪%d",sex,age);
                                 ^
people.m:17:17: warning: class 'people' defined without specifying a base class
      [-Wobjc-root-class]
@implementation people
                ^
people.m:17:23: note: add a super class to fix this problem
@implementation people
                      ^
people.m:29:26: warning: class method '+new' not found (return type defaults to
      'id') [-Wobjc-method-access]
    people *p1 = [people new];
                         ^~~
people.m:30:9: error: 'people' does not have a member named 'age'
    p1->age=20;
    ~~  ^
people.m:31:9: error: 'people' does not have a member named 'sex'
    p1->sex='F';
    ~~  ^


贴出部分错误,
分析了好长时间.

只是在头部引入的标签出错了

结果是解决了,

是应该用# 而不是用@

只是在头部引入的标签出错了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐