Bidesh Sengupta
Bidesh Sengupta

Reputation: 1

How to initialize dynamic array inside a class?

I wish to initialize a multidimensional, dynamic array inside a class. But, I am getting an error.

I have seen several examples on the net. They seem to be difficult. I am new to coding. I would like a simple solution if possible.

class myp
{
    int ntc = 5;
    public:
    double** y = new double*[ntc];
    for(int i = 0; i < ntc; ++i)
        y[i] = new int[3];
};
int main()
{
    int x;
    myp mp;
    mp.y[1][1] = 3;
    cout<<mp.y[1][1]<<endl;;
    return 0;
}

test.cpp:12:2: error: expected unqualified-id before ‘for’
  for(int i = 0; i < ntc; i++)
  ^~~
test.cpp:12:17: error: ‘i’ does not name a type
  for(int i = 0; i < ntc; i++)
             ^
test.cpp:12:26: error: ‘i’ does not name a type
  for(int i = 0; i < ntc; i++)

Upvotes: 0

Views: 103

Answers (2)

Zem
Zem

Reputation: 474

Just For Run.

class myp 
{
    int ntc = 5;
public:
    double **y;

    void initArray()
    {   
        y = new double*[ntc];
        for(int i = 0; i < ntc; ++i)
            y[i] = new double[3]; // i change this line [new int] to [new double]tv
    }   
};

int main()
{
    int x;
    myp mp; 
    mp.initArray();
    mp.y[1][1] = 3;
    cout<<mp.y[1][1]<<endl;;
    return 0;
}

using constructor & destructor

class myp 
{   
    int ntc = 5;
public:  
    double **y;

    myp() // run at created
    {
        y = new double*[ntc];   
        for(int i = 0; i < ntc; ++i)
            y[i] = new double[3];
    }

    ~myp() // run at the end of life cycle 
    {   
        /* free memory here */
    }

};

int main()  
{  
    int x;  
    myp mp;  // myp() called
    mp.y[1][1] = 3;  
    cout<<mp.y[1][1]<<endl;  
    return 0;  
}  

using constructor with parameter, for dynamic size

class myp 
{   
    //    int ntc = 5;  // using at created
public:  
    double **y;

    myp(int ntc, int size) // run at created
    // if you want to use only myp mp;
    // myp(int ntc = 5, int size = 3) {} will be helpful
    {
        y = new double*[ntc];   
        for(int i = 0; i < ntc; ++i)
            y[i] = new double[size];
    }

    ~myp() // run at the end of life cycle 
    {   
        /* free memory here */
    }

};

int main()  
{  
    int x;  
    myp mp(5, 3);  // myp(int, int) called
    mp.y[1][1] = 3;  
    cout<<mp.y[1][1]<<endl;  
    return 0;  
}  

Upvotes: 0

robthebloke
robthebloke

Reputation: 9672

You need to do class initialisation in the constructor function, and cleanup in the destructor.

class myp
{
  int m_numColumns;
  int m_numRows;
  double** y;
public:

  // overload array operators
  double* operator [] (size_t row) { return y[row]; }
  const double* operator [] (size_t row) const { return y[row]; }

  // return dimensions of array
  int numColumns() const { return m_numColumns; }
  int numRows() const { return m_numRows; }

  // constructor
  myp(int nc, int nr) : m_numColumns(nc), m_numRows(nr)
  {
    y = new double*[m_numRows];

    for(int i = 0; i < m_numColumns; ++i)
      y[i] = new int[m_numColumns];
  }

  // destructor
  ~myp()
  {
    for(int i = 0; i < m_numColumns; ++i)
      delete [] y[i];
    delete [] y;
  }

  // be careful of the copy ctor. I'm deleting it in this case!
  myp(const myp&) = delete;

  // edit: as per user4581301's suggestion
  myp() = delete;
  myp(myp&&) = delete; // remove move ctor
  myp& operator = (const myp&) = delete; // remove assignment
  myp& operator = (myp&&) = delete; // remove move assignment
};

int main()
{
   myp mp(5, 3);
   mp[1][1] = 3;
   cout << mp[1][1]<<endl;
   return 0;
}

Upvotes: 2

Related Questions