Qt警告:gcc warning "will be initialized after [-Wreorder]“

7160阅读 0评论2014-09-10 Jan5_Reyn
分类:C/C++

构造函数时,初始化成员变量的顺序要与类声明中的变量顺序相对应,若不对应,则出现如题错误。解决方法就是按照顺序进行初始化。

      对这个问题,StackOverflow上也发生了讨论,以下摘录原文:

Question:

    I am getting a lot of these warnings from 3rd party code that I cannot modify. Is there a way to disable this warning or at least disable it for certain areas (like #pragma push/pop in VC++)?

Example:

list.h:1122: warning: `list >::node_alloc_' will be initialized after 
        list.h:1117: warning:   `allocator<LogOutput*> list<LogOutput*, allocator<LogOutput*> >::alloc_'



Best Answer :

Make sure the members appear in the initializer list in the same order as they appear in the class
Class C { int a; int b; C():b(1),a(2){} //warning, should be C():a(2),b(1) }
or you can turn -Wno-reorder
 
Another Answer:
	

For those using QT having this error, add this to .pro file QMAKE_CXXFLAGS_WARN_ON += -Wno-reorder

原文链接:


上一篇:Qt技巧:Qt智能指针
下一篇:C++里调用exit,内存中的对象没调用析构函数就退出程序了?