指针、二进制无效操作数和菜鸟

发布于 2024-11-10 05:04:10 字数 4108 浏览 0 评论 0原文

[编辑:在底部添加矩形定义。] [编辑2:底部添加了 XYPoint 接口。]

我正在研究一种检查两个矩形是否重叠的方法。 (是的,我在 Kochan 的 Objective-C 编程 中做练习,而且我对此还很陌生。)当我编译这个时,错误消息是:“二进制 + 的操作数无效”。我在第一个 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

[Edit: Rectangle definition added at bottom.]
[Edit2: XYPoint interface added at bottom.]

I'm working on a method that checks if two rectangles overlap. (Yeah, I'm in Kochan's Programming in Objective-C, doing the exercises, and I am painfully new at this.) When I compile this, the error message is: "Invalid operands to binary +". I get it on the first if statement and on the if-else that follows it.

I think I have an issue with pointers, but Kochan doesn't talk about this much.

And, if I take out these lines, the rest of the method works just fine. And the relevant variables are all floating type.

Help?

Also, any other thoughts on the method would be totally welcome. (Like, how do I make lines of code not go out so long. Like I said, painfully new at this.)

-(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];

}

Rectangle Definition:

// Interface, Rectangle Class

@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 interface:

#import <Foundation/Foundation.h>

@interface XYPoint : NSObject
{
    float x;
    float y;
}

@property float x, y;

-(void) setX: (float) xVal andY: (float) yVal;

@end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不离久伴 2024-11-17 05:04:10

您刚刚得到的可能是一个拼写错误:

// 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) && 
                         //^^^This is trying to add an XYPoint,
                         // which is an object, to a float.
   (origin.y + height) >= (r2.origin.y + r2.height) ) 
{
   overlapRectangle = r2;
}

// Test to see if Rectangle 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 && 
                            //^^^Same thing.
         origin.y + height <= r2.origin.y + r2.height ) 
{
...

编译器应该告诉您您要求添加的类型是什么:

错误:二进制 + 的操作数无效(具有“struct XYPoint *”和“float”)

这就是关键。您只需将 r2.origin 更改为 r2.origin.x 即可添加两个浮点数。

至于线条的长度,您可以做两件事。您可以像我一样将条件的每个部分移动到不同的行,但最好为 Rectangle 创建几个方法来为您进行测试。这将使代码更具可读性,因此当您在六个月后返回代码并看到该行内容时:

if( [self containsRectangle:r2] || [self isEqualToRectangle:r2] ){

您将立即知道发生了什么。这里有一些建议:

- (BOOL)containsRectangle:(Rectangle *)otherRect {
    BOOL originBelow = ((origin.x <= otherRect.origin.x) && 
                        (origin.y <= otherRect.origin.y));
    float maxX = origin.x + width;
    float otherMaxX = otherRect.origin.x + otherRect.width;
    BOOL maxXGreater = maxX >= otherMaxX;
    Bfloat maxY = origin.y + height;
    float otherMaxY = otherRect.origin.y + otherRect.height;
    BOOL maxYGreater = maxY >= otherMaxY;
    return originBelow && maxXGreater && maxYGreater;
}

- (BOOL)isEqualToRectangle:(Rectangle *)otherRect {
    BOOL sizeEqual = ((width == otherRect.width) && 
                      (height == otherRect.height));
    return sizeEqual && [origin isEqualToXYPoint:otherRect.origin];

}

注意:我没有测试这些,只是根据 if 的条件将它们粘贴在一起,因此在使用它们之前请仔细检查它们。不过,我确实修复了拼写错误。

请注意,我还在 XYPoint 上编写了一个方法,isEqualToXYPoint:;您也可以实现它,如果两个 XYPointxy 相等,则返回 BOOL

You've just got what is probably a typo:

// 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) && 
                         //^^^This is trying to add an XYPoint,
                         // which is an object, to a float.
   (origin.y + height) >= (r2.origin.y + r2.height) ) 
{
   overlapRectangle = r2;
}

// Test to see if Rectangle 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 && 
                            //^^^Same thing.
         origin.y + height <= r2.origin.y + r2.height ) 
{
...

The compiler should have told you what the types were that you were asking to be added:

error: invalid operands to binary + (have 'struct XYPoint *' and 'float')

that's the key. You just need to change the r2.origin to r2.origin.x so that you're adding two floats.

As for the length of the lines, there's two things you can do. You can move each segment of the conditions to different lines as I've done, but it would probably be best to create a couple of methods for Rectangle that will do the tests for you. This will make the code more readable, so when you come back to it in six months and the line reads:

if( [self containsRectangle:r2] || [self isEqualToRectangle:r2] ){

you'll know what's going on right away. Here's some suggestions for that:

- (BOOL)containsRectangle:(Rectangle *)otherRect {
    BOOL originBelow = ((origin.x <= otherRect.origin.x) && 
                        (origin.y <= otherRect.origin.y));
    float maxX = origin.x + width;
    float otherMaxX = otherRect.origin.x + otherRect.width;
    BOOL maxXGreater = maxX >= otherMaxX;
    Bfloat maxY = origin.y + height;
    float otherMaxY = otherRect.origin.y + otherRect.height;
    BOOL maxYGreater = maxY >= otherMaxY;
    return originBelow && maxXGreater && maxYGreater;
}

- (BOOL)isEqualToRectangle:(Rectangle *)otherRect {
    BOOL sizeEqual = ((width == otherRect.width) && 
                      (height == otherRect.height));
    return sizeEqual && [origin isEqualToXYPoint:otherRect.origin];

}

Note: I didn't test these, just pasted them together from the conditions of your ifs, so double-check them before you use them. I did fix the typo, though.

Notice that I also made up a method on XYPoint here, isEqualToXYPoint:; you can implement that as well, to return a BOOL if the x and y of both XYPoints are equal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文