Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么可以';我的类不能从初始值设定项列表初始化,即使它派生自std::list?_C++_C++11_Initializer List_List Initialization - Fatal编程技术网

C++ 为什么可以';我的类不能从初始值设定项列表初始化,即使它派生自std::list?

C++ 为什么可以';我的类不能从初始值设定项列表初始化,即使它派生自std::list?,c++,c++11,initializer-list,list-initialization,C++,C++11,Initializer List,List Initialization,我有以下代码 #include <utility> #include <list> #include <iostream> class Pair: public std::pair<unsigned int, unsigned int>{ Pair (unsigned int h, unsigned int l) : pair(h,l){}; }; class PairList: public std::list<Pair>{

我有以下代码

#include <utility>
#include <list>
#include <iostream>

class Pair: public std::pair<unsigned int, unsigned int>{
    Pair (unsigned int h, unsigned int l) : pair(h,l){};
};
class PairList: public std::list<Pair>{};


int main(int argc, char** argv){

    PairList pl = {{800,400},{800,400}};
}
#包括
#包括
#包括
类对:公共std::Pair{
Pair(unsignedinth,unsignedintl):Pair(h,l){};
};
类PairList:public std::list{};
int main(int argc,字符**argv){
成对列表pl={800400},{800400};
}
我使用4.6版的MinGW g++和命令行编译它
g++-std=c++0x Test.cpp-o Test.exe

并得到错误:
错误:无法将“{800,400},{800,400}”从“”转换为“PairList”

但是如果在main()中我写
list pl={{800400},{800400}
一切正常。
WTF?

有两种方式:

  • 不要从标准类继承,而是使用
    typedef

    typedef std::pair<unsigned int, unsigned int> Pair;
    typedef std::list<Pair> PairList;
    
    typedef std::pair对;
    typedef std::list PairList;
    
  • 在继承的类中实现正确的构造函数(作为参数),基类构造函数不能自动使用

  • 我推荐第一种选择,因为标准类(很少有例外)不是为继承而设计的。

    有两种方法:

  • 不要从标准类继承,而是使用
    typedef

    typedef std::pair<unsigned int, unsigned int> Pair;
    typedef std::list<Pair> PairList;
    
    typedef std::pair对;
    typedef std::list PairList;
    
  • 在继承的类中实现正确的构造函数(作为参数),基类构造函数不能自动使用


  • 我推荐第一种选择,因为标准类(很少有例外)不是为继承而设计的。

    1。如果我需要用自己的方法扩展基类(特别是pair),该怎么办?2.抱歉,我为PairList实现了构造函数。请参阅我对question@OlegG我刚刚用构造函数需要能够使用初始值设定项列表的参数更新了我的答案。@JoachimPielborg带有
    std::initializer\u list
    的解决方案对我来说很难看。至少现在,@Joachim基类构造函数可以自动使用,如果您使用继承的构造函数(
    使用std::list::list;
    )。但是,我认为目前没有任何编译器实现这些功能。@oleg您需要GCC4.8,请参阅。至于继承问题,你可以私下继承。与公共遗产相比,它更不受欢迎,而且几乎完全安全(即,你需要努力把它搞砸)。如果我需要用自己的方法扩展基类(特别是pair),该怎么办?2.抱歉,我为PairList实现了构造函数。请参阅我对question@OlegG我刚刚用构造函数需要能够使用初始值设定项列表的参数更新了我的答案。@JoachimPielborg带有
    std::initializer\u list
    的解决方案对我来说很难看。至少现在,@Joachim基类构造函数可以自动使用,如果您使用继承的构造函数(
    使用std::list::list;
    )。但是,我认为目前没有任何编译器实现这些功能。@oleg您需要GCC4.8,请参阅。至于继承问题,你可以私下继承。它比公共继承更不受欢迎,而且几乎完全安全(即,你需要努力把它搞糟)。@ExplodingRat在编辑一个糟糕的标题时,简单地修复小的拼写错误并没有多大帮助。建议写一个有意义的标题,不要费心用完美的拼写来获得毫无价值的标题。@ExplodingRat当编辑一个糟糕的标题时,简单地修复小的拼写错误并没有多大帮助。建议写一个有意义的标题,不要费心用完美的拼写写出毫无价值的标题。