C++ c++;构造函数的语法';具有数据成员结构的初始化列表?

C++ c++;构造函数的语法';具有数据成员结构的初始化列表?,c++,C++,可能重复: 编辑: 我最后键入了标题,它给了我一系列相关问题,就像通常一样。在这个列表的底部是完全相同的问题。(使用完全相同的代码;))。 阿拉克完全回答了这个问题,真的。看来我需要投票来结束我自己的问题 嗨 我有一个类似这样的类: class Button { private: SDL_Rect box; public: Button(int x, int y, int w, int h); } box是SDL的一个人。运行带有-Weffc

可能重复:

编辑: 我最后键入了标题,它给了我一系列相关问题,就像通常一样。在这个列表的底部是完全相同的问题。(使用完全相同的代码;))。

阿拉克完全回答了这个问题,真的。看来我需要投票来结束我自己的问题

我有一个类似这样的类:

class Button
{
    private:
        SDL_Rect box;
    public:
        Button(int x, int y, int w, int h);
}
box是SDL的一个人。运行带有-Weffc++的GCC,只是因为我想知道警告是什么样子,抱怨初始化器列表

file.cpp||In constructor 'Button::Button(int, int, int, int)':|
file.cpp|168|error: 'Button::box' should be initialized in the member initialization list|
我想平息一下。不过我还是搞不懂那愚蠢的语法。我试过了

Button::Button(int x, int y, int w, int h ) :
    box(0,0,0,0)
但这只会导致

file.cpp||In constructor 'Button::Button(int, int, int, int)':|
file.cpp|171|error: expected identifier before '{' token|
file.cpp|171|error: member initializer expression list treated as compound expression|
file.cpp|171|error: left-hand operand of comma has no effect|
file.cpp|171|error: right-hand operand of comma has no effect|
file.cpp|171|error: right-hand operand of comma has no effect|
file.cpp|171|error: no matching function for call to 'SDL_Rect::SDL_Rect(int)'|
c:\programming\mingw-4.4.0\bin\..\lib\gcc\mingw32\4.4.0\..\..\..\..\include\SDL\SDL_video.h|50|note: candidates are: SDL_Rect::SDL_Rect(const SDL_Rect&)|
c:\programming\mingw-4.4.0\bin\..\lib\gcc\mingw32\4.4.0\..\..\..\..\include\SDL\SDL_video.h|50|note:                 SDL_Rect::SDL_Rect()|
我试过
box=blah
box.x=blah
box.x(blah)
,但失败了

我还尝试了
box({0,0,0,0})
,以及
box{0,0,0}

file.cpp|169|error: extended initializer lists only available with -std=c++0x or -std=gnu++0x|
file.cpp|171|error: expected identifier before '{' token|
我真的不想编译c++0x,真的。特别是我希望它是跨平台的,我认为很多东西都不支持c++0x

最后,我设法逃脱了:

Button::Button(int x, int y, int w, int h ) :
    box()
{
    box.x = x;
    box.y = y;
    box.w = w;
    box.h = h;
}

这对我来说毫无意义。这是“正确”的方法吗?这与没有初始化器列表的情况不一样吗?

我知道您找到了解决方案,但请注意,您也可以为SDL\u rect编写一个类包装器,甚至编写一个全局函数
SDL\u rect createRect(intx,inty,intw,inth)
好主意。我以后会记住的。