• Advertisement
Sign in to follow this  

Quick help

This topic is 4838 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This matrix class is a little screwy, it is displaying weird numbers
#include <iostream>
#include <cstddef>
#include <vector>

template<typename T> class matrix
{
public:
    matrix(std::size_t r = 4, std::size_t c = 4, T val = T())
    	: rows(r), columns(c)
   	{	
   	    for (std::size_t k = 0; k < rows; k++) 
        {
   	        for (std::size_t j = 0; j < columns; j++) 
            {
   	            cols_vec.clear();
   	        	cols_vec.push_back(val);
   	        }   	
       	    rows_vec.push_back(cols_vec);
       	}
    }
            
    friend std::ostream& operator<<(std::ostream& out, const matrix& m)
    {
        for (std::size_t k = 0; k < m.rows; k++) 
        {
   	        for (std::size_t j = 0; j < m.columns; j++)
   	        	out << m.rows_vec[k][j] << ' ';
       	    out << '\n';
       	}
    }   	
    
    std::vector<T>& operator[](std::size_t i) { return rows_vec; }
    const std::vector<T>& operator[](std::size_t i) const { return rows_vec; }
    
private:
    std::size_t rows, columns;
    std::vector<T> cols_vec;
    std::vector<std::vector<T> > rows_vec;
}; 

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement