【问题标题】:Pointers, Invalid Operands to Binary, and Noobs指向二进制的指针、无效操作数和新手
【发布时间】:2011-05-28 03:48:29
【问题描述】:

[编辑:在底部添加矩形定义。] [Edit2:底部添加了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

【问题讨论】:

  • 报错在哪一行?你能发布你对矩形的定义吗?有什么特别的理由不使用 CGGeometry 中的 CGRect 和 CGRectIntersectsRect(CGRect rect1, CGRect rect2)?
  • 你的width、height和原点坐标的类型是基本类型,比如float?还是一个对象?编辑:哦,你要发布标题。那将回答我的问题。太好了!
  • 好的,现在XYPoint的界面是什么? :)
  • Re: CGRectIntersectsRect -- 这是一个很好的问题,但这些函数仅适用于 Apple 的 CGRect 结构。它们不能在此处与您的自定义类一起使用。
  • 好的,我想我知道出了什么问题。正在寻找答案...

标签: objective-c binary operands


【解决方案1】:

你刚刚得到一个可能是错字的地方:

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

}

注意:我没有测试这些,只是根据你的ifs 的条件将它们粘贴在一起,所以在使用它们之前请仔细检查它们。不过,我确实修正了错字。

注意,我这里XYPoint上也编了一个方法,isEqualToXYPoint:;你也可以实现它,如果x 和y 的XYPoints 相等,则返回BOOL。

【讨论】:

  • 乔希,它奏效了。很抱歉因错字而引起的所有麻烦。我应该抓住它。你建议的其余部分也很有意义。如果我对 NSXMLParser 错误代码有所了解,我会尽力帮助您解决问题,但我认为这可能需要一段时间。再次感谢!
  • 不用道歉;我很乐意提供帮助!祝你好运! (您可以从中记住的一件事是,当您的代码行长而复杂时,很难发现拼写错误 - 尝试将其分解以使其更易于阅读。)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-23
相关资源
最近更新 更多