assignment operator for a 3d array

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2005
Posts: 91
Reputation: tyczj is an unknown quantity at this point 
Solved Threads: 1
tyczj tyczj is offline Offline
Junior Poster in Training

assignment operator for a 3d array

 
0
  #1
Feb 26th, 2006
im not sure how to write an assignment operator for a 3d array this is what i have bit i know its wrong

  1. Cells& Cells::operator= (const Cells& cells)
  2. {
  3. resetSize(cells.DEPTH, cells.ROW, cells.COL);
  4.  
  5. for(int d=0; d<DEPTH; d++)
  6. for(int r=0; r<ROW; r++)
  7. array[d][r] = **this
  8. for(int c=0; c<COL; c++)
  9. array[d][r][c] = cells.array[d][r][c];
  10. return *this;
  11.  
  12. }

am i at least close on what im suppose to do??
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,662
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: assignment operator for a 3d array

 
0
  #2
Feb 26th, 2006
use braces to encose blocks of code. The below may or may not be what you intended.
  1. for(int d=0; d<DEPTH; d++)
  2. {
  3. for(int r=0; r<ROW; r++)
  4. {
  5. array[d][r] = **this
  6. for(int c=0; c<COL; c++)
  7. {
  8. array[d][r][c] = cells.array[d][r][c];
  9. }
  10. return *this;
  11. }
  12. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 91
Reputation: tyczj is an unknown quantity at this point 
Solved Threads: 1
tyczj tyczj is offline Offline
Junior Poster in Training

Re: assignment operator for a 3d array

 
0
  #3
Feb 27th, 2006
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"
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: assignment operator for a 3d array

 
0
  #4
Feb 27th, 2006
Try this.
  1. Cells& Cells::operator= (const Cells& cells)
  2. {
  3. resetSize(cells.DEPTH, cells.ROW, cells.COL);
  4. for(int d=0; d<DEPTH; d++)
  5. {
  6. for(int r=0; r<ROW; r++)
  7. {
  8. for(int c=0; c<COL; c++)
  9. {
  10. array[d][r][c] = cells.array[d][r][c];
  11. }
  12. }
  13. }
  14. return this;
  15. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,662
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: assignment operator for a 3d array

 
0
  #5
Feb 27th, 2006
**this is an illegal dereference of the "this" pointer in c++. What is the intent of that assignment?
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 91
Reputation: tyczj is an unknown quantity at this point 
Solved Threads: 1
tyczj tyczj is offline Offline
Junior Poster in Training

Re: assignment operator for a 3d array

 
0
  #6
Feb 27th, 2006
Originally Posted by Ancient Dragon
**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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,662
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: assignment operator for a 3d array

 
0
  #7
Feb 27th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 91
Reputation: tyczj is an unknown quantity at this point 
Solved Threads: 1
tyczj tyczj is offline Offline
Junior Poster in Training

Re: assignment operator for a 3d array

 
0
  #8
Feb 27th, 2006
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

  1. #include <iostream>
  2. #include "ArrowKeys.h"
  3. using namespace std;
  4.  
  5.  
  6. class Darray{
  7. private:
  8. int DEPTH,
  9. ROW,
  10. COL;
  11. int*** array;
  12.  
  13. public:
  14.  
  15. Darray();
  16. Darray(int depth, int row, int col);
  17. ~Darray();
  18. Darray(const Darray& darray); //copy constructor
  19. Darray& Darray::operator= (const Darray& darray); //assignment operator
  20.  
  21. void getDimensions(int DEPTH, int ROW, int COL);
  22. void destroy();
  23. void create();
  24. void init();
  25. void resetSize(int depth, int row, int col);
  26.  
  27.  
  28. };
  29.  
  30. Darray::Darray()
  31. {
  32. DEPTH=0;
  33. ROW=0;
  34. COL=0;
  35. array=NULL;
  36. }
  37.  
  38. void Darray::create()
  39. {
  40. array = new int** [DEPTH];
  41. for(int d=0; d<DEPTH; d++)
  42. *(array+d) = new int* [ROW];
  43. for (int r=0; r<ROW; r++)
  44. *(*(array+d)+r) = new int [COL];
  45. }
  46.  
  47. Darray::Darray(int depth, int row, int col)
  48. {
  49. DEPTH = depth;
  50. ROW = row;
  51. COL = col;
  52. create();
  53.  
  54. }
  55.  
  56. Darray::~Darray()
  57. {
  58. destroy();
  59. }
  60.  
  61. Darray::Darray(const Darray& darray)
  62. {
  63. DEPTH = darray.DEPTH;
  64. ROW = darray.ROW;
  65. COL = darray.COL;
  66. create();
  67.  
  68. for(int d=0; d<DEPTH; d++)
  69. for(int r=0; r<ROW; r++)
  70. for(int c=0; c<COL; c++)
  71. array[d][r][c] = darray.array[d][r][c];
  72. }
  73.  
  74. Darray& Darray::operator= (const Darray& darray)
  75. {
  76. resetSize(darray.DEPTH, darray.ROW, darray.COL);
  77.  
  78. for(int d=0; d<DEPTH; d++)
  79. for(int r=0; r<ROW; r++)
  80. for(int c=0; c<COL; c++)
  81. array[d][r][c] = darray.array[d][r][c];
  82.  
  83. return *this;
  84. }
  85.  
  86. void Darray::destroy()
  87. {
  88. for (int d=0; d<DEPTH; d++)
  89. {
  90. for(int r=0; r<ROW; r++)
  91. delete [] *(*(array+d)+r);
  92. delete [] *(array+d);
  93. }
  94. delete [] array;
  95. }
  96.  
  97. void Darray::init()
  98. {
  99. for(int d=0; d<DEPTH; d++)
  100. for(int r=0; r<ROW; r++)
  101. for(int c=0; c<COL; c++)
  102. array[d][r][c] = 0;
  103. }
  104.  
  105. void Darray::getDimensions(int DEPTH, int ROW, int COL)
  106. {
  107. cout << "Enter the grid dimensions: " << endl;
  108. cin >> DEPTH >> ROW >> COL;
  109.  
  110. cout << "The Depth is: " << DEPTH << " The Row is: " << ROW
  111. << " The Columns is: " << COL << endl;
  112. }
  113.  
  114. void Darray::resetSize(int depth, int row, int col)
  115. {
  116. destroy();
  117. DEPTH=depth;
  118. ROW=row;
  119. COL=col;
  120. create();
  121. init();
  122. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2734 | Replies: 7
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC