2013-11-24 54 views
-2

我試圖實現在C建設者++,通過使用「捎帶」的方法調用的方法,但在Visual C++編譯器會引發錯誤說:生成器實現

Error 6 error C2143: syntax error : missing ';' before '->' c:\users\owner\desktop\user\t\t\algebratopic.cpp 20 1 MathTutor

使用下面的代碼:

Question * test = new QuestionBuilder() 
     ->withQuestionText("(4y + 5x)2 = ") 
     ->withCorrectAnswer("16y2 + 25x2 + 40xy") 
     ->buildQuestion(); 

每次調用帶*號的方法,返回的問題建設者實例,buildQuestion返回一個問題*對象。

任何想法?

+0

沒有解決問題 – Nick

+1

爲什麼你在這裏使用手動內存管理(和內存泄漏)?此外,這些代碼顯然不足以診斷代碼。發佈最簡單的工作示例來說明問題。 –

回答

0

將括號放在new表達式的周圍,並根據需要將->更改爲.。例如:

struct Foo 
{ 
    Foo & f() { return *this; } 
    Foo & g() { return *this; } 
    Foo * build() { return this; } 
}; 

int main() 
{ 
    Foo * p = (new Foo())->f().g().build(); 
}