Hey, right now I am working on implementing a matrix class for an assignment.

I am using vectors in order to implement it and am having some trouble with adding matrices together.

In my private I have defined:
int nRows, nCols;
vector<int> data;

Here is my code for the operator+;

matrix<T> operator+(const matrix<T> & other) const {
    int other_rows = other.rows();
    int other_cols = other.cols();
    data.resize(other_rows*other_cols);
    vector<int> other_data ((other_rows*other_cols));  //needs to take in data

    for (int j=0;j<nCols;j++) {
      for (int i=0;i<nRows;i++) {
	data.push_back(data.at(i) + other_data.at(i));
      }
    }
    return *this;
  }

Right now I am getting an error that there is no matching function for call... on my lines with:
- data.resize(...)
- vector<int> other_data
- in my nested for loop, data.push_back(...)

Edited 6 Years Ago by sgriffin: Fixed previous issue

Your function prototype is constant corrected, so that means you
cannot change the member variables, which you should not have to.

First you need to check if both the matrix are of equal size( equal row AND equal column ). Then next, you should create a new matrix object, that has a size equal to the matrix passed in. Then in a for loop, like you
were doing, just set the value at the index to the addition of the matrices at the same index.

Your function prototype is constant corrected, so that means you
cannot change the member variables, which you should not have to.

First you need to check if both the matrix are of equal size( equal row AND equal column ). Then next, you should create a new matrix object, that has a size equal to the matrix passed in. Then in a for loop, like you
were doing, just set the value at the index to the addition of the matrices at the same index.

For this function it assumes that matrix this and matrix other have the same dimensions, so I do not believe I will need an if statement to check.

Right now here is my code:

matrix<T> operator+(const matrix<T> & other) const {
    vector<T> s_row(nRows);    // Vector of a single row of other
    
    for (int j=0;j<nCols;j++) {
      for (int i=0;i<nRows;i++) {
	this->data.push_back (this->data[i] + s_row[i]);
      }
    }    
    return *this;
  }

I am still getting an error for data.push_back and I'm not sure why. When I compile it is saying that their is no matching function....

>>matrix<T> operator+(const matrix<T> & other) const {

You're still not getting it. You have const corrected function header. That
means you cannot change your member variables state. Which means
this->data.push_back (this->data + s_row); is illegal!!!!!

Hey, sorry I have been here in a bit, some other work came up and this got a bit side tracked. So I created a new matrix with others data and used a new vector to store the addition.

I am not sure how I should be returning the matrix because I have a vector with all the information, but need to return a matrix.

matrix<T> operator+(const matrix<T> & other) const {
    
    // Create new matrix with other's data
    matrix<int> new_data = matrix(other.rows(), other.cols());
    vector<int> s_row (new_data.rows());
    vector<int> addVec(nRows);

    for (int j=0;j<nCols;j++) {
      for (int i=0;i<nRows;i++) {
	addVec.push_back(this->data[i] + s_row[i]);
      }
    }    
    return addVec;
  }

Edited 6 Years Ago by sgriffin: Change in the code

A matrix is routinely defined as a 2 dimensional structure, like a table of data. There's not reason it can't be defined otherwise, but that would be like overloading the - operator to do addition. Please, post a declaration of your matrix class so we can see how it relates to a STL vector object.

A matrix is routinely defined as a 2 dimensional structure, like a table of data. There's not reason it can't be defined otherwise, but that would be like overloading the - operator to do addition. Please, post a declaration of your matrix class so we can see how it relates to a STL vector object.

Here is my code leading up to this current function with nRows, nCols, and a vector data in private:

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

template <class T>
class matrix {

public:
  // PRE:  None
  // POST: Create a matrix with 0 rows and 0 columns
  matrix() {
    nRows = 0;
    nCols = 0;
    data.resize(0);
  }

  // PRE: r and c are nonnegative
  // POST: Create a matrix with r rows and c columns.
  matrix(int r,int c) {
    nRows = r;
    nCols = c;
    data.resize(nRows*nCols);
  }

  // PRE: (other is a matrix)
  // POST: this is a new copy of other
  matrix(const matrix<T> & other) {
    nRows = other.rows();
    nCols = other.cols();
    data.resize(nRows*nCols);
  }

  // PRE: (this is a matrix)
  // POST: this matrix is destroyed.  All memory is deleted.
  ~matrix() {
    data.clear();
  }

  // PRE: other is a matrix, this is a matrix.  The dimensions 
  //      might be different.
  // POST: make this matrix look exactly like other.
  matrix<T> & operator=(const matrix<T> & other) {
    int cRows = other.rows();
    int cCols = other.cols();
    this->data.resize(cRows,cCols);
    return *this;
  }

  // PRE: this is a matrix with r+1 rows and c+1 columns
  // POST: output this matrix to ostr, all on one line, like this:
  //       [v00,v01,..,v0c;v10,v11,...,v1c; ... ;vr0,vr1,...,vrc]
  //       for example, here's the output of a 2x3 vector of ints:
  //       [5,2,6;8,9,-1]
  void output(ostream & ostr) const {
    ostr << "[";
    int c,r;
    for (c = 0;c<nCols;c++) {
      for (r = 0;r<nRows-1;r++) {
	ostr << data.at(r);
	ostr << ",";	
      }
      ostr << data.back();
      ostr << ";";
    }
    ostr << "]"; 
  }

  // PRE: this and other are matrices with the same dimensions
  // POST: the sum of the matrices is returned.
  //       matrix addition is achieved by adding corresponding
  //       elements.  For example,
  //       [1,4,2;6,4,1] + [3,2,6;7,6,8] = [4,6,8;13,10,9].
  matrix<T> operator+(const matrix<T> & other) const {
    
    // Create new matrix with other's data
    //   matrix<int> new_data = matrix(other.rows(), other.cols());
    vector<int> s_row;
    s_row.assign (nRows,other.rows());
    vector<int> addVec(nRows);

    for (int j=0;j<nCols;j++) {
      for (int i=0;i<nRows;i++) {
	addVec.push_back(this->data[i] + s_row[i]);
      }
    }    
    return *this;
  }

where is the data vector in you matrix class definition. I'm seeing you using it in your constructors but its never defined. also in you operator= function you aren't copy the data from the other matrix. all you are doing is changing the size of the vector.

Like I said, those were all defined in my private section below:

private:
  int nRows, nCols;
  vector<int> data;
};

As for the copy, I believe it is just necessary to copy the dimensions of the other matrix and allot the necessary memory for later, but not the actual data of the matrix.

why is that? do you want to write matrix a = matrix b and then use another function or line of code to actual set the data in a to the data in b?

Edited 6 Years Ago by NathanOliver: n/a

Are you going to want to handle cases where nRows is more than 1 as you suggest in the comments for the output() function? If so you should test the output() function with something like a 2x3 matrix because I think you will find it buggy.

Presumably the decision not to post class member variable declarations was a consciously made. Correct?

Edit: Ok, I need to type and not get distracted.

Edited 6 Years Ago by Lerner: n/a

Ok final question which I believe will solve all my problems and I will finally solve my problem.

This may seem stupid, but I'm just a beginner...

How can I read the data out of a matrix so that I can use its data later.

so if I have:

matrix<T> operator+(const matrix<T> & other) const {

How can I read the data in 'other'>

right now I have created a new matrix<matrix <int> > new_data and have it so it is reading like this:

vector<vector<int> > addVec(nRows);

    vector<vector<int> > addVec(nCols*nRows);
    matrix<matrix<int> > new_data = other;

    for (int j=0;j<nCols;j++) {
      for (int i=0;i<nRows;i++) {
	addVec[j][i] = this->data[j][i] + new_data[j][i];
      }
    }

But it is giving me the error:

error: no match for 'operator[]' in 'new_data[j]'

Can someone explain the situation?

>>How can I read the data in 'other'

Using the . operator if the underlying data is declared with public access and with public access functions if not. Maybe something like:

other.table[row][col];

or, depending on declaration, maybe something like:

other.getCell(row, col);

Lines 1, 3, and 4 make me uncomfortable, but maybe it's just because I haven't used user defined template classes for a long time. Nevertheless, it seems strange to have two variables called addVec and to use matrix<matrix<> >. It would seem more comfortable to me to use:

matrix<T> resultMatrix;

and to use

resultMatrix.table[j] = table[j] + other.table[j];

or something to that effect if table held the actual "data" in the matrix object and table were declared with public access.

>>How can I read the data in 'other'

Using the . operator if the underlying data is declared with public access and with public access functions if not. Maybe something like:

other.table[row][col];

or, depending on declaration, maybe something like:

other.getCell(row, col);

Lines 1, 3, and 4 make me uncomfortable, but maybe it's just because I haven't used user defined template classes for a long time. Nevertheless, it seems strange to have two variables called addVec and to use matrix<matrix<> >. It would seem more comfortable to me to use:

matrix<T> resultMatrix;

and to use

resultMatrix.table[j] = table[j] + other.table[j];

or something to that effect if table held the actual "data" in the matrix object and table were declared with public access.

DUH, thanks so much. That works perfectly. I had other.data and data.other mixed up for some reason when I was trying that.

This question has already been answered. Start a new discussion instead.