Click here to Skip to main content
15,877,967 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am trying to create a 3D matrix of objects of a user defined type, and let the use to input the dimensions of the matrix

What I have tried:

I tried 3D array, and this is solve the issue of using a user defined type, but the size can't be input by user.

I tried to use vectors but this didn't allow me to use the user defined type

my user defined type is
C++
class spin_cite
{
public:
    int val;
    int x;
    int y;
    int z;
    bool check;
    spin_cite(int X, int Y, int Z, int VAL, bool CHECK)
    {
        x = X;
        y = Y;
        z = Z;
        val = VAL;
        check = CHECK;
    }
};

I want to declare my 3D matrix in a global scope before the main method, and inside the main method, I want to initialize the 3D matrix, using the user input and assign values

I was able to do this easily in C#, but my C# code is not fact enough, that is why i am trying to do this in C++
Posted
Updated 29-Nov-22 7:59am
v2
Comments
CPallini 29-Nov-22 6:03am    
What so you mean with "I tried to use vectors but this didn't allow me to use the user defined type"?
std::vector is a container, it can hold objects of user-defined type.

you can use vector<user_defined_type>, but you should offer a default constructor,
e.g.:

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


class spin_cite
{
public:
	spin_cite() {}
	int val;
	int x;
	int y;
	int z;
	bool check;
	spin_cite(int X, int Y, int Z, int VAL, bool CHECK)
	{
		x = X;
		y = Y;
		z = Z;
		val = VAL;
		check = CHECK;
	}
};

using elem = vector<spin_cite>;
using elem_arr = vector<elem>;
vector<elem_arr> a;

int main()
{
	cout << "Set dims like: x,y,z" << endl;

	short x = 0, y = 0, z = 0;
	((cin >> x).ignore(1, ',') >> y).ignore(1, ',') >> z;

	cout << "alloc a 3d array: " << x << ',' << y << ',' << z << endl;

	for (short ix = 0; ix < x; ++ix)
	{		 
		elem_arr ea(y);
		for (int iy = 0; iy < y; ++iy)
		{
			elem e(z);
			for (int iz = 0; iz < z; ++iz)
			{
				// access every unit's members
				e[iz].x = 1;
				e[iz].y = 1;
				e[iz].z = 1;
				e[iz].val = 1;
				e[iz].check = 1;
			}
			ea[iy] = std::move(e);
		}

		a.push_back(std::move(ea));
	}

	cout << "a.size == " << a.size() 
		<< ", a[0].size == " << a[0].size() 
		<< ", a[0][0].size== " << a[0][0].size() 
		<< endl;

	return 0;
}
 
Share this answer
 
You have been given the answer already in your previous question at Allocate 3D matrix in C[^]. Please do not repost the same question.
 
Share this answer
 
Richard is right, the answer was pretty much given previously. That can be easily adapted to be a template function using a user-defined type. It would be best to use new instead of the macro and then in the release function delete should be used to accommodate the user's type.

If you really don't see how to do that then let me know (right here) and I will help you with it.

Incidentally, I would have written it in C++ originally but you had specified C.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900