我想要一個構造函數在其初始化列表中調用其他構造函數,因爲我沒有重複的邏輯。這是我的.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
我究竟做錯了什麼?
謝謝!
也許你的編譯器不支持委託構造函數呢?你需要像GCC 4.9或MSVC13。 –
你使用什麼編譯器?有可能你的編譯器(或你有的版本)不支持委託構造函數(這確實是C++ 11的一個特性)。 – RageD
OT:爲了上帝的緣故,man,const string和這些參數。現在代碼中有足夠的不必要的字符串複製。 – WhozCraig