Multidimensional Dyamic Array in class

U

undbund

Hi, i am learing how to create a multidimensional dynamic array.I want
to initialize the array in the constructor. I have tried looking for
the solution in this news group but none of them were helpful.
Here is the code:

#include <iostream>
#include <conio>
using namespace std;

class Mat
{

int (*mat)[3][3];
public:
Mat::Mat();
void printArray();
};

Mat::Mat()
{
(*mat)[3][3] = new int[3][3];
for( int r = 0; r < 3; r++ )
{
for( int c = 0; c < 3; c++ )
{
(*mat)[r][c] = r * 2;
}
}
}

void Mat::printArray()
{
for( int i = 0; i < 3; i++ )
{
for( int j = 0; j < 3; j++ )
{
cout << mat[j] << " ";
}
cout << endl;
}
}

int main()
{
Mat m;
m.printArray();
getch();
return 0;
}

I get the following error:
Error E2034 D:\programming\matrix.cpp 16:
Cannot convert 'int ( *)[3]' to 'int' in function Mat::Mat()

How to solve this error?

thanks for any help
undbund
 
J

John Harrison

undbund said:
Hi, i am learing how to create a multidimensional dynamic array.I want
to initialize the array in the constructor. I have tried looking for
the solution in this news group but none of them were helpful.

Corrections below.
Here is the code:

#include <iostream>
#include <conio>
using namespace std;

class Mat
{

int (*mat)[3][3];

int (*mat)[3];
public:
Mat::Mat();
void printArray();
};

Mat::Mat()
{
(*mat)[3][3] = new int[3][3];

mat = new int[3][3];
for( int r = 0; r < 3; r++ )
{
for( int c = 0; c < 3; c++ )
{
(*mat)[r][c] = r * 2;

mat[r][c] = r*2;
}
}
}

void Mat::printArray()
{
for( int i = 0; i < 3; i++ )
{
for( int j = 0; j < 3; j++ )
{
cout << mat[j] << " ";
}
cout << endl;
}
}

int main()
{
Mat m;
m.printArray();
getch();
return 0;
}

I get the following error:
Error E2034 D:\programming\matrix.cpp 16:
Cannot convert 'int ( *)[3]' to 'int' in function Mat::Mat()

How to solve this error?


See above.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top