繁体   English   中英

Objective-C自定义类

[英]Objective-C custom class

我得到错误:

VertexPoint.m:12:1:属性'color'的类型('GLfloat *'(aka'float *'))与实例变量'_color'('float [4]')的类型不匹配

VertexPoint.m:12:1:属性“位置”的类型(“ GLfloat *”(又名“ float *”))与实例变量“ _position”(“ float [3]”)的类型不匹配

我应该怎么解决呢?

这是代码:

//
//  VertexPoint.m

#import <Foundation/Foundation.h>
#import "VertexPoint.h"

@implementation VertexPoint

- (id)init {
     self = [super init];
    if (self) {

        _position[0] = 1;
        _position[1] = 2;
        _position[2] = 3;

        _color[0] = 1;
        _color[1] = 2;
        _color[2] = 3;
        _color[3] = 4;
    }
    return self;

};

- (GLfloat *)x {
    return &_position[0];
};
- (GLfloat *)y {
    return &_position[1];
};
- (GLfloat *)z {
    return &_position[2];
};
- (GLfloat *)r {
    return &_color[0];
};
- (GLfloat *)g {
    return &_color[1];
};
- (GLfloat *)b {
    return &_color[2];
};
- (GLfloat *)a {
    return &_color[3];
};

- (void)setPosition:(GLfloat *)pos {
    for (NSUInteger i = 0; i < 3; i++)
        _position[i] = pos[i];
};
- (void)setColor:(GLfloat *)col{
    for (NSUInteger i = 0; i < 4; i++)
        _color[i] = col[i];
};

@end

//  VertexPoint.h
//


#import <Foundation/Foundation.h>
#import <OpenGLES/EAGL.h>
#import <OpenGLES/ES1/gl.h>
#import <OpenGLES/ES1/glext.h>

@interface VertexPoint : NSObject
{
    GLfloat _position[3];
    GLfloat _color[4];
}
@property(nonatomic,readwrite) GLfloat *position;
@property(nonatomic,readwrite) GLfloat *color;
@property(nonatomic,readwrite) GLfloat *x;
@property(nonatomic,readwrite) GLfloat *y;
@property(nonatomic,readwrite) GLfloat *z;
@property(nonatomic,readwrite) GLfloat *r;
@property(nonatomic,readwrite) GLfloat *g;
@property(nonatomic,readwrite) GLfloat *b;
@property(nonatomic,readwrite) GLfloat *a;

- (GLfloat *)x;
- (GLfloat *)y;
- (GLfloat *)z;
- (GLfloat *)r;
- (GLfloat *)g;
- (GLfloat *)b;
- (GLfloat *)a;
-(void)setColor:(GLfloat *)col;
-(void)setPosition:(GLfloat *)pos;
@end

重新更新(2)

新的h和m文件:

#import <Foundation/Foundation.h>
#import <OpenGLES/EAGL.h>
#import <OpenGLES/ES1/gl.h>
#import <OpenGLES/ES1/glext.h>

@interface VertexPoint : NSObject
{
    GLfloat _position[3];
    GLfloat _color[4];
}
@property(nonatomic,readwrite) GLfloat *position;
@property(nonatomic,readwrite) GLfloat *color;
@property(nonatomic,readonly) GLfloat x;
@property(nonatomic,readonly) GLfloat y;
@property(nonatomic,readonly) GLfloat z;
@property(nonatomic,readonly) GLfloat r;
@property(nonatomic,readonly) GLfloat g;
@property(nonatomic,readonly) GLfloat b;
@property(nonatomic,readonly) GLfloat a;

- (GLfloat)x;
- (GLfloat)y;
- (GLfloat)z;
- (GLfloat)r;
- (GLfloat)g;
- (GLfloat)b;
- (GLfloat)a;
-(void)setColor:(GLfloat *)col;
-(void)setPosition:(GLfloat *)pos;
@end

#import <Foundation/Foundation.h>
#import "VertexPoint.h"

@implementation VertexPoint

- (id)init {
     self = [super init];
    if (self) {

        _position[0] = 1.0f;
        _position[1] = 1.0f;
        _position[2] = 1.0f;

        _color[0] = 1.0f;
        _color[1] = 1.0f;
        _color[2] = 1.0f;
        _color[3] = 1.0f;
    }
    return self;

};

- (GLfloat)x {
    return _position[0];
};
- (GLfloat)y {
    return _position[1];
};
- (GLfloat)z {
    return _position[2];
};
- (GLfloat)r {
    return _color[0];
};
- (GLfloat )g {
    return _color[1];
};
- (GLfloat )b {
    return _color[2];
};
- (GLfloat )a {
    return _color[3];
};

- (void)setPosition:(GLfloat *)pos {
    for (NSUInteger i = 0; i < 3; i++)
        _position[i] = pos[i];
};
- (void)setColor:(GLfloat *)col{
    for (NSUInteger i = 0; i < 4; i++)
        _color[i] = col[i];
};

@end

原始错误消息仍然存在。

加:

- (GLfloat *)color {
    return &_color[0];
}

- (GLfloat *)position {
    return &_position[0];
}

到您的.m实施文件。

发生的事情是,您已经使用GLfloat *类型声明了colorposition属性,并且有名为_color_position实例变量。 编译器希望通过标准的属性-ivar命名约定将它们自动链接在一起,但是这些类型不匹配(编译器抱怨是因为在这些目的上,浮点指针和浮点数组不是同一件事;您不能在C中返回一个数组。)

如果您显式添加与属性相对应的getter方法,则可以自己显式提供映射(和其他类型),如我在此处所示。 在这两种情况下,我仅返回了指向数组中第一个元素的指针。 在这种情况下,调用方将只需要简单地知道颜色数组包含四个元素,位置一个包含三个元素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM