我的功能链不想工作,为什么?
我有以下类:我的功能链不想工作,为什么?
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 '->'
有没有办法得到这个工作?
这是一个operator precedence问题。尝试
// Add some brackets
wndRect = (new GLRectangle())->centerRect(400, 160);
(wndRect = new GLRectangle())->centerRect(400, 160);
但为什么要这样做?为什么不提供参数构造函数,所以你可以说:
wndRect = new GLRectangle(400, 160);
我想你可能错放你第一个括号 – 2010-01-03 15:36:50
构造也将工作,但我想,如果我能保持代码的自我记录和自我解释。也许我应该考虑:wndRect =(新的GLRectangle(400,160)) - > center(); //嗯:) – 2010-01-03 15:38:56
@Gab你为什么这么认为? – 2010-01-03 15:42:15
谢谢,它修复了它! – 2010-01-03 15:37:09