| | |
assignment operator for a 3d array
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2005
Posts: 91
Reputation:
Solved Threads: 1
im not sure how to write an assignment operator for a 3d array this is what i have bit i know its wrong
am i at least close on what im suppose to do??
C++ Syntax (Toggle Plain Text)
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??
use braces to encose blocks of code. The below may or may not be what you intended.
C++ Syntax (Toggle Plain Text)
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; } }
•
•
Join Date: Mar 2005
Posts: 91
Reputation:
Solved Threads: 1
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"
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.
C++ Syntax (Toggle Plain Text)
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; }
•
•
Join Date: Mar 2005
Posts: 91
Reputation:
Solved Threads: 1
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
C++ Syntax (Toggle Plain Text)
#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(); }
![]() |
Similar Threads
- copy constructor & assignment operator overload problems (C++)
- Assignment operator help (C++)
- Overloading assignment operator (C++)
- "Safe array" assignment (C++)
Other Threads in the C++ Forum
- Previous Thread: Script problem help
- Next Thread: home work help please
Views: 2734 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project python random read recursion recursive reference return sort stream string strings struct studio system template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






