-2

I need to inherit all functions from std::vector and I want to overload operators to make a complete matrix class. There is not too much documentation on this topic.

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
}

When I want to initialize it, I got an error.

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>>'
JeJo
  • 30,635
  • 6
  • 49
  • 88
dani2442
  • 41
  • 7
  • 4
    You probably don't want to do this – Hatted Rooster May 03 '18 at 19:04
  • 1
    First, be careful when inheriting from standard containers, they are not intended for that. Second, you have `private` inheritance. – François Andrieux May 03 '18 at 19:22
  • I don't think inheriting privately from vector is that much of a problem. Regarding your question, could you be more specific about the problem you have? The error message gives you a pretty good indication of the problem. – juanchopanza May 03 '18 at 19:24
  • 1
    try `class Matriz: public vector` – Alexander Oh May 03 '18 at 19:27
  • 2
    Check out this question on whether inheriting from `std::vector` is a good idea: https://stackoverflow.com/questions/4353203/thou-shalt-not-inherit-from-stdvector – RAM May 03 '18 at 19:33

2 Answers2

0

This should work:

#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 << " ";
}
JeJo
  • 30,635
  • 6
  • 49
  • 88
0

Inherit from vector class is a mistake

It is very difficult and produces a lot of errors as it is said here.
I have made a class which has a vector and inlcudes:

  • template
  • operator overloading
  • matrix operations

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

};
dani2442
  • 41
  • 7