im not sure how to write an assignment operator for a 3d array this is what i have bit i know its wrong

Cells& Cells::operator= (const Cells& cells)
{
	resetSize(cells.DEPTH, cells.ROW, cells.COL);

	for(int d=0; d<DEPTH; d++)
		for(int r=0; r<ROW; r++)
			array[d][r] = **this
			for(int c=0; c<COL; c++)
				array[d][r][c] = cells.array[d][r][c];
			return *this;
			
}

am i at least close on what im suppose to do??

Recommended Answers

All 7 Replies

use braces to encose blocks of code. The below may or may not be what you intended.

for(int d=0; d<DEPTH; d++)
{
		for(int r=0; r<ROW; r++)
                {
			array[d][r] = **this
			for(int c=0; c<COL; c++)
                        {
				array[d][r][c] = cells.array[d][r][c];
                         }
			return *this;
                }
}

no i still get errors with brackets

with brackets i get:
error C2100: illegal indirection.

error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class Darray' (or there is no acceptable conversion).

without i get:
error C2100: illegal indirection.

error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class Darray' (or there is no acceptable conversion)

error C2065: 'r' : undeclared identifier.

they point to where it says "array[d][r] = **this"

Try this.

Cells& Cells::operator= (const Cells& cells)
{
	resetSize(cells.DEPTH, cells.ROW, cells.COL);
	for(int d=0; d<DEPTH; d++)
        {
		for(int r=0; r<ROW; r++)
                {
			for(int c=0; c<COL; c++)
                        {
				array[d][r][c] = cells.array[d][r][c];
                        }
                 }
         }
         return this;		
}

**this is an illegal dereference of the "this" pointer in c++. What is the intent of that assignment?

**this is an illegal dereference of the "this" pointer in c++. What is the intent of that assignment?

i thought i needed it to get a pointer of a 2d array then another pointer for the 3d array to make dynamic.

the this pointer is not a pointer to an array but a pointer to a specific instance of the entire class. you are mis-using the this pointer. You need to post more code in order for anyone to show you how to do whatever it is you want to do.

here is my whole code but i think i figured it out though. the code has what i changed in it. and i see what your saying now though

#include <iostream>
#include "ArrowKeys.h"
using namespace std;


class Darray{
private:
		int DEPTH,
			  ROW,
			  COL;
		int*** array;

public:
	
	Darray();
	Darray(int depth, int row, int col);
	~Darray();
	Darray(const Darray& darray);						//copy constructor
	Darray& Darray::operator= (const Darray& darray);	//assignment operator

	void getDimensions(int DEPTH, int ROW, int COL);
	void destroy();
	void create();
	void init();
	void resetSize(int depth, int row, int col);


};

Darray::Darray()
{
	DEPTH=0;
	ROW=0;
	COL=0;
	array=NULL;
}

void Darray::create()
{
	array = new int** [DEPTH];
	for(int d=0; d<DEPTH; d++)
		*(array+d) = new int* [ROW];
	for (int r=0; r<ROW; r++)
		*(*(array+d)+r) = new int [COL];
}

Darray::Darray(int depth, int row, int col)
{
	DEPTH = depth;
	ROW = row;
	COL = col;
	create();

}

Darray::~Darray()
{
	destroy();
}

Darray::Darray(const Darray& darray)
{
	DEPTH = darray.DEPTH;
	ROW = darray.ROW;
	COL = darray.COL;
	create();

	for(int d=0; d<DEPTH; d++)
		for(int r=0; r<ROW; r++)
			for(int c=0; c<COL; c++)
				array[d][r][c] = darray.array[d][r][c];
}

Darray& Darray::operator= (const Darray& darray)
{
	resetSize(darray.DEPTH, darray.ROW, darray.COL);

	for(int d=0; d<DEPTH; d++)
		for(int r=0; r<ROW; r++)
			for(int c=0; c<COL; c++)
				array[d][r][c] = darray.array[d][r][c];
			
			return *this;	
}

void Darray::destroy()
{
	 for (int d=0; d<DEPTH; d++)
	 {
		 for(int r=0; r<ROW; r++)
			 delete [] *(*(array+d)+r);
		 delete [] *(array+d);
	 }
	 delete [] array;
 }

void Darray::init()
{
	for(int d=0; d<DEPTH; d++)
		for(int r=0; r<ROW; r++)
			for(int c=0; c<COL; c++)
				array[d][r][c] = 0;
}

void Darray::getDimensions(int DEPTH, int ROW, int COL)
{
	cout << "Enter the grid dimensions: " << endl;
	cin >> DEPTH >> ROW >> COL;

	cout << "The Depth is: " << DEPTH << "  The Row is: " << ROW
		<< "  The Columns is: " << COL << endl;
}

void Darray::resetSize(int depth, int row, int col)
{
	destroy();
	DEPTH=depth;
	ROW=row;
	COL=col;
	create();
	init();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.