Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.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::vector继承的类_C++_Class_C++11_Inheritance_Stdvector - Fatal编程技术网

C++ 如何创建从std::vector继承的类

C++ 如何创建从std::vector继承的类,c++,class,c++11,inheritance,stdvector,C++,Class,C++11,Inheritance,Stdvector,我需要从std::vector继承所有函数,并且我想重载操作符以生成一个完整的矩阵类。 关于这个主题没有太多文档 Matriz.h #include <vector> #include <iostream> using namespace std; template<typename T> class Matriz:vector<T> { public: using vector<T>::vector; private: }

我需要从
std::vector
继承所有函数,并且我想重载操作符以生成一个完整的
矩阵
类。 关于这个主题没有太多文档

Matriz.h

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

template<typename T>
class Matriz:vector<T>
{
public:
    using vector<T>::vector;
private:
}
#包括
#包括
使用名称空间std;
模板
类矩阵:向量
{
公众:
使用vector::vector;
私人:
}
Matriz.cpp

int main()
{
    Matriz<int> dani;
    dani.push_back(2); //Here is the error and I don`t know what it happens
}
intmain()
{
Matriz dani;
丹尼。推回(2);//这是错误,我不知道发生了什么
}
当我想初始化它时,我得到了一个错误

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>>'
严重性代码描述项目文件行抑制状态
错误C2247“std::vector::push_back”不可访问,因为“Matriz”使用“private”从“std::vector”继承
这应该可以:

#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 << " ";
}
#包括
#包括
模板
类Matriz:public std::vector
{
公众:
使用std::vector::vector;
私人:
};
int main()
{
Matriz dani;
丹尼。推回(2);
丹尼。推回(3);
用于(康斯特汽车和it公司:丹尼)
std::cout从vector类继承是一个错误
正如所说的那样,这是非常困难的,并且会产生很多错误。
我制作了一个类,它有一个向量,包含:

  • 模板
  • 运算符重载
  • 矩阵运算
链接:

#pragma一次
#包括
#包括
模板
类向量
{
公众:
向量();
向量(int);
向量(int,int);
~Vector();
std::向量v;
bool Check_Size();
模板布尔检查大小(标准::向量&);
模板bool Check_Size_Fast(std::vector&);
作废打印();
无效转座();
void逆();
void Inverse2();
void Inverse3();
模板
friend std::向量运算符*(常数Q,向量);
模板
friend std::向量运算符*(向量,常数Q);
模板
friend std::向量运算符*(向量和,向量和);
模板
friend std::向量运算符+(向量和,向量和);
模板
向量运算符-(向量&,向量&);
向量和运算符=(const std::Vector&v)
{
这个->v=v;
归还*这个;
}
标准::向量和运算符+=(向量和v){
返回v+(*此);
}
标准::向量和运算符-=(向量和v){
返回v-(*此项);
}
标准::向量和运算符*=(向量和v){
返回v*(*本);
}
私人:
无效递归检查(std::vector&);
};

您可能不想这样做。首先,从标准容器继承时要小心,它们不是用于此目的的。其次,您有
private
继承。我不认为从vector私下继承有多大问题。关于您的问题,您能更具体地说明您遇到的问题吗?错误是我消息为您提供了一个很好的问题指示。请尝试
class Matriz:public vector
查看这个问题,了解从
std::vector继承是否是一个好主意:
#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>&);

};