2011-05-28 38 views
1

[編輯:加入在底部矩形定義] [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]; 

} 

矩形定義:

//接口,Rectangle類

@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 
+3

在報告錯誤的哪一行?你可以發表你的定義的矩形? CGGeometry中不使用CGRect和CGRectIntersectsRect(CGRect rect1,CGRect rect2)的任何特定原因? – highlycaffeinated 2011-05-28 03:59:47

+0

錯誤顯示在第一個* if *行和第一個* if-else *行上。我將抓住矩形定義。 – ctaggart 2011-05-28 04:07:54

+1

「寬度」,「高度」和原點座標的類型是基本類型,比如「float」?還是一個對象?編輯:哦,你要發佈標題。這將回答我的問題。大! – 2011-05-28 04:11:58

回答

3

你拿到什麼可能是一個錯字:

// 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) 
{ 
... 

編譯器應該告訴你什麼類型爲你被要求加入:

error: invalid operands to binary + (have 'struct XYPoint *' and '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 S的條件,所以在使用之前仔細檢查他們。不過,我確實修復了錯字。

請注意,我也在XYPoint這裏編了一個方法,isEqualToXYPoint:;如果兩個XYPoint之間的xy相等,您也可以實現該功能,以返回BOOL

+0

喬希,它的工作。抱歉,所有這一切打亂了打字錯誤。我應該抓住那個。你所建議的其餘部分也是非常有意義的。如果我知道有關NSXMLParser錯誤代碼的任何信息,我會嘗試幫助解決您的問題,但我認爲這可能會持續一段時間。再次感謝! – ctaggart 2011-05-28 04:50:00

+0

無需道歉;我很樂意提供幫助!祝你好運! (您可以記住的一件事是,當您的代碼行很長且很複雜時,很難發現拼寫錯誤 - 嘗試將事情分解以便閱讀。) – 2011-05-28 04:53:03

+0

好的教訓要學習。會做! – ctaggart 2011-05-28 05:06:22

相關問題