2013-01-06 28 views
1

我想要一個構造函數在其初始化列表中調用其他構造函數,因爲我沒有重複的邏輯。這是我的.h文件看起來是這樣的:C++不讓我在初始化列表中使用其他構造函數

class Button : public Component 
{ 
public: 
    Button(int x, int y, int width, int height, string normalSrc, string hoverSrc, string downSrc); 
    Button(int x, int y, int width, int height, string normalSrc, string hoverSrc, string downSrc, Uint8 r, Uint8 g, Uint8 b); 
    Button(int x, int y, int width, int height, string src) : Button(x, y, width, height, src, src, src) { } 
    Button(int x, int y, int width, int height, string src, Uint8 r, Uint8 g, Uint8 b) : Button(x, y, width, height, src, src, src, r, g, b) { } 
    ~Button(); 

但是當我嘗試編譯(使用-std = C++ 0x中作爲一個額外的編譯器標誌)我得到這個錯誤:

In file included from /home/villages/Desktop/ogam-january-pipes/src/Vesper/Ui/Button.cpp:8:0: 
/home/villages/Desktop/ogam-january-pipes/src/Vesper/Ui/Button.h: In constructor ‘Button::Button(int, int, int, int, std::string)’: 
/home/villages/Desktop/ogam-january-pipes/src/Vesper/Ui/Button.h:29:60: error: type ‘Button’ is not a direct base of ‘Button’ 
/home/villages/Desktop/ogam-january-pipes/src/Vesper/Ui/Button.h: In constructor ‘Button::Button(int, int, int, int, std::string, Uint8, Uint8, Uint8)’: 
/home/villages/Desktop/ogam-january-pipes/src/Vesper/Ui/Button.h:30:87: error: type ‘Button’ is not a direct base of ‘Button’ 
make[2]: *** [CMakeFiles/Villages.dir/src/Vesper/Ui/Button.cpp.o] Error 1 
make[1]: *** [CMakeFiles/Villages.dir/all] Error 2 
make: *** [all] Error 2 

我究竟做錯了什麼?

謝謝!

+7

也許你的編譯器不支持委託構造函數呢?你需要像GCC 4.9或MSVC13。 –

+0

你使用什麼編譯器?有可能你的編譯器(或你有的版本)不支持委託構造函數(這確實是C++ 11的一個特性)。 – RageD

+2

OT:爲了上帝的緣故,man,const string和這些參數。現在代碼中有足夠的不必要的字符串複製。 – WhozCraig

回答

5

What am I doing wrong?

您正在使用錯誤的編譯器/版本。 委託構造函數功能是一個C++ 11功能,並不是所有編譯器都趕上了。

根據this tableGCC支持它,因爲4.7因爲3.0MSVC因爲2012年11月CTP

+0

CTP是什麼意思btw? – billz

+1

@billz:_Community Technology Preview_ –

+0

我正在使用cmake make文件。我應該設置一些特定的設置嗎? – will

相關問題