2010-01-03 37 views
0

我有以下類:我的功能鏈不想工作,爲什麼?

GLRectangle.h

#include "XPView.h" 

class GLRectangle 
{ 
public: 
    int top, left, bottom, right; 

public: 
    GLRectangle(void); 
    ~GLRectangle(void); 
    GLRectangle* centerRect(int rectWidth, int rectHeight, int boundWidth=0, int boundHeight=0); 
}; 

GLRectangle.cpp

#include "GLRectangle.h" 

GLRectangle::GLRectangle(void) 
{ 
} 

GLRectangle::~GLRectangle(void) 
{ 
} 

GLRectangle* GLRectangle::centerRect(int rectWidth, int rectHeight, int boundWidth, int boundHeight) 
{ 
    if(boundWidth == 0) 
    { 
     boundWidth = XPView::getWindowWidth(); 
    } 

    if(boundHeight == 0) 
    { 
     boundHeight = XPView::getWindowHeight(); 
    } 

    // Set rectangle attributes 
    left = boundWidth/2 - rectWidth/2; 
    top = boundHeight/2 + rectHeight/2; 
    right = boundWidth/2 + rectWidth/2; 
    bottom = boundHeight/2- rectHeight/2; 

    return this; 
} 

,我試圖鏈的功能上的建設對象如下:

wndRect = new GLRectangle()->centerRect(400, 160); 

但得到以下錯誤:

error C2143: syntax error:missing ';' before '->' 

有沒有辦法得到這個工作?

回答

2

這是一個operator precedence問題。嘗試

// Add some brackets 
wndRect = (new GLRectangle())->centerRect(400, 160); 
+0

謝謝,它修復了它! – 2010-01-03 15:37:09

1
(wndRect = new GLRectangle())->centerRect(400, 160); 

但爲什麼要這樣做?爲什麼不提供參數構造函數,所以你可以說:

wndRect = new GLRectangle(400, 160); 
+0

我想你可能錯放你第一個括號 – 2010-01-03 15:36:50

+0

構造也將工作,但我想,如果我能保持代碼的自我記錄和自我解釋。也許我應該考慮:wndRect =(新的GLRectangle(400,160)) - > center(); //嗯:) – 2010-01-03 15:38:56

+0

@Gab你爲什麼這麼認爲? – 2010-01-03 15:42:15

相關問題