C ++不让我在初始化列表中使用其他构造函数

时间:2013-01-06 23:24:07

标签: c++ c++11 constructor

我正在尝试让构造函数在其初始化列表中调用其他构造函数,因为我没有重复的逻辑。这是我的.h文件的样子:

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

我做错了什么?

谢谢!

1 个答案:

答案 0 :(得分:5)

  

我做错了什么?

您使用的是错误的编译器/版本。 委托构造函数功能是 C ++ 11 功能,并非所有编译器都已赶上。

根据this table GCC 支持 4.7 Clang ,因为 3.0 ,以及< em> MSVC 自 2012年11月CTP

相关问题