0

I'm trying to make a Matrix program that makes a n*m matrix of integers using vectors of vectors of integers. But I'm having trouble doing this as I'm fairly new to C++. I have started implementing my program, but I'm getting a whole bunch of errors that I can't figure out why. Also, this is my 1st time using a .h and vectors so please be gentle :) .

Matrix.h

#ifndef MATRIX_H
#define MATRIX_H 

#include<vector>
using namespace std;

class Matrix{
    public:
        Matrix( );
        Matrix(int r, int c);
        void setRow(vector<int> row, int r);
        void setColumn(vector<int> col, int c);
        int getRow();
        int getCol();
        void output();
        double average();
    private:
        int row, column;
        vector< vector<int> > matrix;
};

#endif

Matrix.cpp

#include "Matrix.h"
#include<iostream>
#include<vector>
using namespace std;

int main(){
    int row,column;
    cout << "Enter number of rows: ";
    cin >> row;
    cout << "Enter number of column: ";
    cin >> column;
    Matrix matrix(row,column);
    matrix.output();

}

Matrix::Matrix():row(3), column(3){
    matrix = vector<vector<int> >(row);
    for (int i = 0; i < row; i++){
        matrix[i] = vector<int>(column);
    }
}

Matrix::Matrix(int r, int c): row(r), column(c){
    matrix = vector<vector<int> >(r);
    for (int i = 0; i < r; i++){
        matrix[i] = vector<int>(c);
    }
}

void Matrix::setRow(vector<int> row, int r){
    if (r <= row){
        if (row.size <= column){
            for (int i = 0; i < row.size; i++){
                matrix[r][i] = row[i];
            }
        }
    }
}

void Matrix::setColumn(vector<int> col, int c){
    if (c <= column){
        if (col.size <= row){
            for (int i = 0; i < col.size; i++){
                matrix[i][c] = col[i];
            }
        }
    }
}

void Matrix::output(){
    for (int i = 0; i < row; i++){
        for (int j = 0; j < column; j++){
            cout << matrix[i][j];
        }
        cout<< endl;
    }
}

OK so the above is the fixed version of my previous code, but now I'm getting this error:

Errors:

Matrix.cpp: In member function ‘void Matrix::setRow(std::vector<int, std::allocator<int> >, int)’:
Matrix.cpp:32: error: no match for ‘operator<=’ in ‘r <= row’
Matrix.cpp:33: error: invalid use of member (did you forget the ‘&’ ?)
Matrix.cpp:34: error: invalid use of member (did you forget the ‘&’ ?)
Matrix.cpp: In member function ‘void Matrix::setColumn(std::vector<int, std::allocator<int> >, int)’:
Matrix.cpp:43: error: invalid use of member (did you forget the ‘&’ ?)
Matrix.cpp:44: error: invalid use of member (did you forget the ‘&’ ?)
eddie
  • 1,252
  • 3
  • 15
  • 20
a22asin
  • 53
  • 2
  • 9
  • 1
    You don't need to have that `#include` in the header and it will slow down your compiles times by a lot since `iostream` is such a huge header. – sjdowling Nov 02 '14 at 16:53
  • Important information: [Naming Include Guards](http://stackoverflow.com/q/4867559/103167) and [What are the rules about using an underscore in a C++ identifier?](http://stackoverflow.com/q/228783/103167) – Ben Voigt Nov 02 '14 at 16:58
  • You should never put `using namespace std;` in the global part of a header file (and preferably not anywhere else). – Galik Nov 02 '14 at 17:22
  • @Galik, so i should remove it, but do i need to include it anywhere else in the .h file? – a22asin Nov 02 '14 at 17:25
  • You can put it in your class definition or else put your class in its own namespace and add it in there. Or you can just qualify your `std::vector` with `std::`. – Galik Nov 02 '14 at 17:39
  • Please review: http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – Galik Nov 02 '14 at 17:42

3 Answers3

1

when you define Matrix, you define it to nothing, so class Matrix{ evaluates into class{, as well as all occurances of Matrix inside the class itself.

This is because of preprocessor running the #if blocks before compiling code, and replacing all occurances of found #defines into given strings, which is in your case, empty string.

Creris
  • 1,128
  • 9
  • 20
1

Please change your header gaurds first:-

#ifndef Matrix
#define Matrix

it resembles class name. make it something like

#ifndef __MATRIX_
#define __MATRIX_
ravi
  • 10,994
  • 1
  • 18
  • 36
  • 1
    But don't use double underscores or underscore followed by capital letter, because those are reserved names. – Ben Voigt Nov 02 '14 at 16:55
  • 2
    @a22asin: Yes it would... until you come across code that uses `M` as a variable name, and suddenly breaks. I left a comment on the question with a link to good recommendations. – Ben Voigt Nov 02 '14 at 17:03
1

You have a few issues here:

  1. You named the include guard and the class with the same name. As such, the preprocessor will completely mess up by replacing Matrix with the empty string. Replace:

    #ifndef Matrix
    #define Matrix
    

    with:

    #ifndef Matrix_h
    #define Matrix_h
    

    or anything else. Trailing _H and ALL CAPS are a common expedient to avoid naming collisions. For instance, you could use:

    #ifndef MATRIX_H
    #define MATRIX_H
    
  2. You forgot to import Vector class. Add at the top of Matrix.h:

    #include <vector>
    
  3. You can't initialize a vector of vectors with matrix(0,0,0,0,0,0). Just omit that part. int elements in a vector already default to zero.

  4. You did not provide a constructor which accepts an integer and a vector, which would be used at line 20 of Matrix.cpp:

    Matrix::Matrix(int r, int c): row(r), column(c){
        matrix(r, vector<int>(c));
        // ....
    }
    

    Fix it like:

    Matrix::Matrix(int r, int c): row(r), column(c){
        matrix = vector<vector<int> >(r);
        for (int i = 0; i < r; i++){
            matrix[i] = vector<int>(c);
        }
    }
    
Stefano Sanfilippo
  • 32,265
  • 7
  • 79
  • 80