简体   繁体   中英

Designing .h and a .cpp file C++ : Errors

I'm trying to make a Matrix program that makes an*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 ‘&’ ?)

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 #define s into given strings, which is in your case, empty string.

Please change your header gaurds first:-

#ifndef Matrix
#define Matrix

it resembles class name. make it something like

#ifndef __MATRIX_
#define __MATRIX_

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM