开发者

Pass a struct to a class method

开发者 https://www.devze.com 2023-02-24 03:52 出处:网络
I wanted to be able to do this: vec2 pos(1,5); myActor.position = [Coord positionFrom: pos ]; Using my structure:

I wanted to be able to do this:

vec2 pos(1,5);
myActor.position = [Coord positionFrom: pos ];

Using my structure:

typedef float32 float;
struct vec2 {
    vec2(float32 xx, float32 yy) {
        x = xx;
        y = yy;
    }
    float32 x, y;
};

Coord.h

@interface Coord : NSObject {
}
+(CGPoint) positionFrom: (vec2) pos;
+(CGPoint) positionFromAngle: (float32) angle;

Coord.mm

#import "Coord.h"
@implementation Coord
+(CGPoint) positionFrom:(vec2) pos {
    return CGPointMake( pos.x * 32, pos.y * 32);
}
+(CGPoint) positionFromA开发者_如何学运维ngle:(float32) angle {
    return CGPointMake( cos(angle) * 32, cos(angle) * 32);
}
@end

But I get these errors (in Coord.h, on the positionFrom lines):

Expected ')' before 'vec2'
Expected ')' before 'float32'


This should be:

+(CGPoint) positionFrom: (struct vec2) pos;

Or else you have failed to import the header file that defines vec2.

You also have a trailing ] in the return statement of positionFrom:.

BTW, just stylistically, this kind of thing is usually done with a function rather than a class method. Something like this:

CGPoint CGPointFromVec2( struct vec2 pos );

That makes it parallel to CGPointFromString().

0

精彩评论

暂无评论...
验证码 换一张
取 消