C ++ Error on overloading << using vectors

I am trying to map a base 3 by 3 matrix using vectors, and the <operator. The problem is that when I try to run the code, I get an error that there is no name named size in the MatrixViaVector.

In my header file, I:

#ifndef homework7_MatrixViaVector_h
#define homework7_MatrixViaVector_h

#include<iostream>
#include<fstream>
#include<string>
#include <cstdlib>
#include <vector>

using namespace std;
template <class T>

class MatrixViaVector{

public:
    MatrixViaVector();
    MatrixViaVector(int m,int n);
    template <class H>
    friend ostream& operator <<(ostream& outs, const MatrixViaVector<H> &obj);
private:
    int m,n;
    vector<vector<T>> matrix;
};

#endif

      

And in my test file I have:

    #include "MatrixViaVector.h"

    template <class T>
    MatrixViaVector<T>::MatrixViaVector(){

        //creates a 3 by 3 matrix with elements equal to 0


        for (int i=0;i<3;i++){
            vector<int> row;
            for (int j=0;j<3;j++)
                row.push_back(0);
            matrix.push_back(row);
        }
    }

template <class T>
MatrixViaVector<T>::MatrixViaVector(int m,int n)//creates a m by n matrix????
{
    //creates a matrix with dimensions m and n with elements equal to 0

    for (int i=0;i<m;i++){
        vector<int> row;
        for (int j=0;j<n;j++)
            row.push_back(0);
        matrix.push_back(row);
    }
}

    template <class T>
    ostream& operator <<(ostream& outs, const MatrixViaVector<T> & obj)
    {
        //obj shud have the vector and therefore I should be able to use its size

        for (int i = 0; i < obj.size(); i++){
            for (int j = 0; j < obj.capacity(); j++)
                outs << " "<< obj.matrix[i][j];
            outs<<endl;
        }
        outs<<endl;
        return outs;
    }

    int main()
    {

    MatrixViaVector <int> A;
    MatrixViaVector <int> Az(3,2);//created an object with dimensions 3by2????
    cout<<A<<endl;
    cout<<Az<<endl;//this prints out a 3 by 3 matrix which i dont get????????
    }

      

+3


source share


1 answer


Yours MatrixViaVector<>

doesn't have a function size()

, but if you're going to use the vector size, do this:

Modify this snippet:

for (int i = 0; i < obj.size(); i++){
        for (int j = 0; j < obj.capacity(); j++)
            outs << " "<< obj.matrix[i][j];
        outs<<endl;
    }

      



For

for (std::vector<int>::size_type i = 0; i < obj.matrix.size(); i++){
        for (std::vector<int>::size_type j = 0; j < obj.matrix.size(); j++)
            outs << " "<< obj.matrix[i][j];
        outs<<endl;
    }

      

std :: vector :: size () and std :: vector :: capacity () are two different functions, please check its difference.

+2


source







All Articles