指针,无效的二进制操作数和Noobs
|
[编辑:在底部添加矩形定义。]
[Edit2:在底部添加了XYPoint界面。]
我正在研究一种方法,该方法检查两个矩形是否重叠。 (是的,我正在使用Objective-C的Kochan编程,正在做练习,对此我感到很痛苦。)编译此文件时,错误消息是:\“无效的操作数到二进制+ \” 。我在第一个if语句及其后的if-else中得到它。
我认为我对指针有问题,但是Kochan并没有太多谈论。
而且,如果我删除这些行,则该方法的其余部分都可以正常工作。并且相关的变量都是浮点型的。
救命?
同样,完全欢迎您对该方法提出任何其他想法。 (例如,我如何使代码行不会持续太长时间。就像我说的那样,这很痛苦。)
-(void) overlap: (Rectangle *)r2
{
overlapRectangle = [[Rectangle alloc] init];
leftRectangle = [[Rectangle alloc] init];
rightRectangle = [[Rectangle alloc] init];
lowerRectangle = [[Rectangle alloc] init];
upperRectangle = [[Rectangle alloc] init];
BOOL xIntersect = NO;
BOOL yIntersect = NO;
// Test to see if the Rectangle contains, or is equal to, Rectangle b
if (origin.x <= r2.origin.x && origin.y <= r2.origin.y && (origin.x + width) >= (r2.origin + r2.width) && (origin.y + height) >= (r2.origin.y + r2.height) )
{
overlapRectangle = r2;
}
// Test to see if Retangle b contains, or is equal to, the Rectangle
else if (origin.x >= r2.origin.x && origin.y >= r2.origin.y && origin.x + width <= r2.origin + r2.width && origin.y + height <= r2.origin.y + r2.height )
{
overlapRectangle = self;
}
// I should add tests for triangles overlapping on three
// sides or overlapping on two sides, but I\'m not going
// to right now. Just learning objects and methods.
// Test to see if rectangles overlap on the x-axis
// Current is an if, because I wanted to run the code below
// to see if it worked, and it did.
if (origin.x <= r2.origin.x)
{
leftRectangle = self;
rightRectangle = r2;
}
else
{
rightRectangle = self;
leftRectangle = r2;
}
if (rightRectangle.origin.x + rightRectangle.width > leftRectangle.origin.x)
{
xIntersect = YES;
}
// Test to see if rectangles overlap on the y-axis
if (origin.y <= r2.origin.y)
{
lowerRectangle = self;
upperRectangle = r2;
}
else
{
lowerRectangle = self;
upperRectangle = r2;
}
if (lowerRectangle.origin.y + lowerRectangle.height > upperRectangle.origin.y)
{
yIntersect = YES;
}
// If retangles overlap on both the x-axis and y-axis,
// determination of overlapping rectangle\'s origin, height, and width
// and display same.
if (xIntersect == YES && yIntersect == YES)
{
overlapRectangle.origin.y = upperRectangle.origin.y;
overlapRectangle.origin.x = rightRectangle.origin.x;
overlapRectangle.height = lowerRectangle.height - (upperRectangle.origin.y - lowerRectangle.origin.y);
overlapRectangle.width = leftRectangle.width - (rightRectangle.origin.x - leftRectangle.origin.x);
NSLog (@\"Your rectangles overlap.\");
NSLog (@\"Rectangle: w = %g, h = %g\", overlapRectangle.width, overlapRectangle.height);
NSLog (@\"Area = %g, Perimeter = %g\", [overlapRectangle area], [overlapRectangle perimeter]);
NSLog (@\"Origin at (%g, %g)\", overlapRectangle.origin.x, overlapRectangle.origin.y);
}
else
{
NSLog (@\"Your rectangles do not overlap.\");
}
[overlapRectangle autorelease];
[leftRectangle autorelease];
[rightRectangle autorelease];
[lowerRectangle autorelease];
[upperRectangle autorelease];
}
矩形定义:
//接口,矩形类
@interface Rectangle : NSObject
{
float width;
float height;
XYPoint *origin;
// For overlapping calculations
Rectangle *overlapRectangle;
Rectangle *leftRectangle;
Rectangle *rightRectangle;
Rectangle *lowerRectangle;
Rectangle *upperRectangle;
}
@property float width, height;
-(XYPoint *) origin;
-(void) setOrigin: (XYPoint *) pt;
-(void) setWidth: (float) w andHeight: (float) h;
-(float) area;
-(float) perimeter;
-(void) print;
-(void) translate;
-(void) overlap: (Rectangle *)r2;
-(void) draw;
@end
XYPoint界面:
#import <Foundation/Foundation.h>
@interface XYPoint : NSObject
{
float x;
float y;
}
@property float x, y;
-(void) setX: (float) xVal andY: (float) yVal;
@end
没有找到相关结果
已邀请:
1 个回复
田眯衅
编译器应该已经告诉您要添加的类型是什么: 错误:对二进制+无效的操作数(具有\'struct XYPoint * \'和\'float \') 这就是关键。您只需要将
更改为
,即可添加两个浮点数。 至于行的长度,您可以做两件事。您可以像我一样将条件的每个部分移到不同的行,但是最好为ѭ6创建两个方法来为您做测试。这将使代码更具可读性,因此当您在六个月后再次使用它时,该行显示为:
您会马上知道发生了什么。这里有一些建议:
注意:我没有测试它们,只是将它们粘贴到9英镑的条件下,所以在使用它们之前请仔细检查它们。我确实解决了错字。 注意,我还在这里的
,
上构造了一个方法;您也可以实现这一点,如果两个ѭ10的13和ѭ14相等,则返回ѭ12。