如何创建继承自 std::vector 的 class

How to create a class which inheritates from std::vector

我需要继承 std::vector 的所有功能,我想 重载运算符 来制作完整的 matrix class。 关于此主题的文档不多。

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>>'

这应该有效:

#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 << " ";
}

从向量继承class是个错误

据说很难,而且会产生很多错误here
我制作了一个 class ,它有一个矢量和 inlcudes:

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

LINK : 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>&);

};