Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

c++ - constructor in Matrix class,two arguments:m and n; allocate memory for the Array* data and every element of that array has to have m elements

I have two classes: Array and Matrix.

#ifndef MATRIX_H
#define MATRIX_H
#include "Array.h"

class Matrix
{
protected:
    Array* data;
    int n,m;
public:
    Matrix(int n, int m);
    friend class Array;
    ~Matrix();


   };

  #endif // MATRIX_H

Array.h :

#ifndef ARRAY_H
#define ARRAY_H
using namespace std;

class Array
{
protected:
    int* data;
    int m;
public:
    Array(int m);
    Array& operator=(Array &a);
    Array& operator +=(Array& a);
    virtual void setElem(int pos, int value){
        int pom = 0;
        for (int i=0; i<this->m; i++){
            if (i==pos) {
                    this->data[i]=value;
                    pom = 1;
            }
        }
        if (pom==0) throw 1;
    };


    ~Array();
};

#endif // ARRAY_H

Now I have to write a constructor in Matrix class which has two arguments: int n and int m. I have to allocate memory for the Array* data, which will have n elements, and every element of that array has to have m elements. This is what I did:

Matrix::Matrix(int n, int m)
{
this->n = n;
this->m = m;
this->data = new Array[this->n];
for (int i=0; i<this->n; i++){
    this->data[i].data = new int[m];
}
}

Error is - no matching function for call to 'Array::Array()'. And also error - 'int* Array::data' is protected. What do I do?

question from:https://stackoverflow.com/questions/65918437/constructor-in-matrix-class-two-argumentsm-and-n-allocate-memory-for-the-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...