0

In the following program mat(i, j) indexing isn't working in Matrix class:

#include <iostream>
#include <stdexcept>

class Vector {
private:
    int size;
    double* data;

public:
    // Constructor
    Vector() : size(0), data(nullptr) {}
    Vector(int s) : size(s), data(new double[s])
    {
        //std::cout << "Hello, Vector!";
    }
    ~Vector() {
        delete[] data;
    }
    // Copy constructor
    Vector(const Vector& other) : size(other.size), data(new double[other.size]) {
        for (int i = 0; i < size; i++) {
            data[i] = other.data[i];
        }
    }

    // Copy assignment operator
    Vector& operator=(const Vector& other) {
        if (this != &other) {
            delete[] data;
            size = other.size;
            data = new double[size];
            for (int i = 0; i < size; i++) {
                data[i] = other.data[i];
            }
        }
        return *this;
    }

    // Accessor methods
    int getSize() const {
        return size;
    }

    double& operator[](int i) {
        return data[i];
    }    
};

class Matrix {
private:
    int rows;
    int cols;
    Vector* data;

public:
    // Constructor
    Matrix() : rows(0), cols(0), data(nullptr) {}

    // Constructor
    Matrix(int rows_, int cols_) : rows(rows_), cols(cols_), data(new Vector[rows_]) {
        for (int i = 0; i < rows_; i++) {
            data[i] = Vector(cols_);
        }
    }

    // Destructor
    ~Matrix() {
        delete[] data;
    }

    // Copy constructor
    Matrix(const Matrix& other) : rows(other.rows), cols(other.cols), data(new Vector[other.rows]) {
        for (int i = 0; i < rows; i++) {
            data[i] = other.data[i];
        }
    }

    // Copy assignment operator
    Matrix& operator=(const Matrix& other) {
        if (this != &other) {
            delete[] data;
            rows = other.rows;
            cols = other.cols;
            data = new Vector[rows];
            for (int i = 0; i < rows; i++) {
                data[i] = other.data[i];
            }
        }
        return *this;
    }

    // Accessor methods
    int getRows() const {
        return rows;
    }

    int getCols() const {
        return cols;
    }

    double &operator()(int i, int j) {
        return data[i][j];
    }

    

    Matrix transpose() const {
        Matrix result(cols, rows);
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                result(j, i) = data[i][j];
            }
        }
        return result;
    }

    void print(){
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                std::cout << data[i][j] << " ";
            }
            std::cout << std::endl;
        }
    }
};

int main(){
  Matrix m(2, 3);
    m(0,0) = 1;     m(0,1) = 2;     m(0,2) = 3;
    m(1,0) = 4;     m(1,1) = 5;     m(1,2) = 6;

    m.print();

    Matrix result = m.transpose();
    result.print();

  if(m(0,1) != 4){
    std::cout<<"Indexing is not working";
    std::cout<<std::endl;
  }
}

Output:

$ sh -c make -s
$ ./main
1 2 3 
4 5 6 
1 4 
2 5 
3 6 
Indexing is not working

What is missing here?

user366312
  • 16,949
  • 65
  • 235
  • 452
  • Why is `Vector *data;` a pointer in the `Matrix` class? Why not simply `Vector data;`? You've defeated the whole purpose of allowing `Vector` to handle the dynamically allocated memory. Also, if you had `Vector data;`, you don't need a user-defined destructor, assignment operator, or copy constructor in the `Matrix` class. And a more basic question: why are you not using `std::vector`? – PaulMcKenzie Jul 20 '23 at 07:05
  • provide an `operator()(std::size_t row, std::size_t column)`. [Multi dimensional index operators](https://en.cppreference.com/w/cpp/language/operators#Array_subscript_operator) will not appear until C++23 – Pepijn Kramer Jul 20 '23 at 07:07
  • Have you tried to use a [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to see what really happens? – Some programmer dude Jul 20 '23 at 07:08
  • 1
    By the way, in `m(0,1) != 4` are you perhaps supposed to use `result(0,1) != 4`? – Some programmer dude Jul 20 '23 at 07:09
  • `m(0,1) != 4` But `m(0,1) = 2`. – KamilCuk Jul 20 '23 at 07:11

0 Answers0