2010-01-03 5 views
0

J'ai la classe suivante:Ma chaîne de fonctions ne veut pas fonctionner, pourquoi?

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; 
} 

et je suis en train d'enchaîner une fonction sur la construction du s'opposer comme suit:

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

mais obtenir l'erreur suivante:

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

est-il un moyen d'obtenir ce travail?

Répondre

2

Il y a un problème operator precedence. Essayez

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

Merci, qui l'a réparé! –

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

mais pourquoi? Pourquoi ne pas fournir des paramètres à votre constructeur afin que vous puissiez dire:

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

Je pense que vous pourriez avoir vous égaré première parenthèse –

+0

Le constructeur va également travailler, mais je veux garder le code auto-documenté et explicite si je peux. Peut-être que je devrais considérer: wndRect = (nouveau GLRectangle (400, 160)) -> center(); // Mmmm :) –

+0

@Gab Pourquoi pensez-vous que? –

Questions connexes