将结构传递给类方法
我希望能够做到这一点:
vec2 pos(1,5);
myActor.position = [Coord positionFrom: pos ];
使用我的结构:
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) positionFromAngle:(float32) angle {
return CGPointMake( cos(angle) * 32, cos(angle) * 32);
}
@end
但我收到这些错误(在 Coord.h 中的positionFrom行上):
Expected ')' before 'vec2'
Expected ')' before 'float32'
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) positionFromAngle:(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'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该是:
或者你无法导入定义
vec2
的头文件。positionFrom:
的return
语句中还有一个尾随]
。顺便说一句,从风格上来说,这种事情通常是用函数而不是类方法来完成的。像这样的东西:
这使得它与 CGPointFromString() 并行。
This should be:
Or else you have failed to import the header file that defines
vec2
.You also have a trailing
]
in thereturn
statement ofpositionFrom:
.BTW, just stylistically, this kind of thing is usually done with a function rather than a class method. Something like this:
That makes it parallel to
CGPointFromString()
.