【问题标题】:How to create a class which inheritates from std::vector如何创建一个继承自 std::vector 的类
【发布时间】:2018-10-14 04:29:18
【问题描述】:

我需要从std::vector 继承所有函数,并且我想重载运算符 以创建一个完整的matrix 类。 没有太多关于这个主题的文档。

Matriz.h

#include <vector>
#include <iostream>
using namespace std;

template<typename T>
class Matriz:vector<T>
{
public:
    using vector<T>::vector;
private:
}

Matriz.cpp

int main()
{
    Matriz<int> dani;
    dani.push_back(2); //Here is the error and I don`t know what it happens
}

当我想初始化它时,我得到了一个错误。

Severity    Code    Description Project File    Line    Suppression State
Error   C2247   'std::vector<int,std::allocator<_Ty>>::push_back' not accessible because 'Matriz<int>' uses 'private' to inherit from 'std::vector<int,std::allocator<_Ty>>'

【问题讨论】:

  • 你可能不想这样做
  • 首先,从标准容器继承时要小心,它们不是为此而设计的。其次,你有private 继承。
  • 我不认为从 vector 私有继承是个大问题。关于您的问题,您能否更具体地说明您遇到的问题?错误消息可以很好地指示问题。
  • 试试class Matriz: public vector&lt;T&gt;
  • 看看这个问题是否从std::vector继承是个好主意:stackoverflow.com/questions/4353203/…

标签: c++ class c++11 inheritance stdvector


【解决方案1】:

这应该可行:

#include <vector>
#include <iostream>

template<typename T>
class Matriz: public std::vector<T>
{
public:
   using std::vector<T>::vector;

private:
};

int main()
{
   Matriz<int> dani;
   dani.push_back(2);
   dani.push_back(3);

   for(const auto& it: dani)
      std::cout << it << " ";
}

【讨论】:

    【解决方案2】:

    从向量类继承是错误的

    正如here所说,这非常困难并且会产生很多错误。
    我制作了一个有向量的类,包括:

    • 模板
    • 运算符重载
    • 矩阵运算

    链接:vector.h

    #pragma once
    
    #include <vector>
    #include <iostream>
    
    template<class T>
    class Vector
    {
    public:
        Vector();
        Vector(int);
        Vector(int, int);
    
        ~Vector();
    
        std::vector<std::vector<T>> v;
    
        bool Check_Size() ;
        template<typename T1> bool Check_Size(std::vector<T1>&) ;
        template<typename T1> bool Check_Size_Fast(std::vector<T1>&);
    
        void Print();
    
        void Transponse();
    
        void Inverse();
        void Inverse2();
        void Inverse3();
    
    
        template<class T,class Q>
        friend std::vector<std::vector<T>> operator* (const Q , Vector<T> );
        template<class T,class Q>
        friend std::vector<std::vector<T>> operator* (Vector<T> , const Q );
        template<class T>
        friend std::vector<std::vector<T>> operator*(Vector<T>& , Vector<T>&);
        template<typename T>
        friend std::vector<std::vector<T>> operator+(Vector<T> &, Vector<T> &);
        template<typename T>
        friend std::vector<std::vector<T>> operator-(Vector<T> &, Vector<T> &);
    
    
    
        Vector<T>& operator = (const std::vector<std::vector<T>>& v)
        {
            this->v = v;
            return *this;
        }
    
        std::vector<std::vector<T>>& operator +=( Vector<T>&v) {
            return v + (*this);
        }
    
        std::vector<std::vector<T>>& operator -=(Vector<T>&v) {
            return v - (*this);
        }
    
        std::vector<std::vector<T>>& operator *=(Vector<T>&v) {
            return v * (*this);
        }
    
    
    private:
    
        void Recursive_Check(std::vector<T>&);
    
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-23
      • 1970-01-01
      • 2017-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-05
      相关资源
      最近更新 更多