【问题标题】:Error while inheriting std::vector继承 std::vector 时出错
【发布时间】:2015-04-01 08:53:42
【问题描述】:

我正在尝试定义一个从 std::vector 类继承的类。下面是代码

 template<class t>
 class Vector:vector<t>
 {
   public:
    using vector<t>::vector;
 };

 int main(int argc, char *argv[])
 {
    Vector<int> v;
    return 0;
 }

我收到这样的错误:

   "error: 'std::vector<t, std::allocator<_CharT> >::vector' names constructor"

所以基本上我想知道为什么我的程序失败了,以及从父 std::vector 类继承的类中要重载的所有程序。

提前致谢。

【问题讨论】:

  • std::vector 继承时,请注意它不必定义虚拟析构函数。因此,从它继承通常不是一个好主意。
  • @Bathsheba 这是私有继承,所以我怀疑缺少虚拟析构函数是否重要。
  • @KeillRandor public 在这里不相关。
  • @tobi303 大概你的意思是虚拟析构函数?没有虚拟构造函数这样的东西。
  • @tobi303 有些人认为私有继承组合。

标签: c++ inheritance vector


【解决方案1】:

我试过了:

 #include <vector>

 template<class t>
 class Vector: std::vector<t>
 {
    public:
        using std::vector<t>::vector;
 };

 int main()
 {
    Vector<int> v;
    return 0;
 }

当传递标志 -std=c++11 时,它在 gcc 4.8. 上运行良好。

听起来你的编译器无法弄清楚你想用using 指令完成什么。由于继承构造函数是 C++11 的一项特性,我建议您确保您在 C++11 模式下运行编译器

【讨论】:

  • 有时候你不能改变你的编译器,所以你必须想办法让代码适应编译器
猜你喜欢
  • 2015-10-27
  • 2015-10-11
  • 1970-01-01
  • 2018-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多