Solved

(Axter) Your 2D Array Code

Posted on 2004-10-12
13
227 Views
Last Modified: 2010-04-01
I'm trying to access a member function of a 2D array class I created with your header file, and while trying to write the data in it out to a file using member functions, the first one [0][0] wrote out just fine, but the second one [1][0] freaked out. What's going on with this?
0
Comment
Question by:SonicX_BlueBlur
  • 6
  • 4
  • 3
13 Comments
 
LVL 17

Expert Comment

by:rstaveley
ID: 12286840
[He's going to ask you to post some code.]
0
 

Author Comment

by:SonicX_BlueBlur
ID: 12287366
(Okay!)

This code uses the 2D array:
for (int Y = 0; Y < bHeight; Y++)
{
      for (int X = 0; X < bWidth; X++)
      {
            BoardTile[X][Y]->WriteData(boardFile);
      }
}
BoardTile is the 2D array. bWidth and bHeight are int; they hold the width and height for the array.

This goes to:
void BoardData::WriteData(FILE *boardFile)
{
      fwrite(&Color1, sizeof(int), 1, boardFile);
      fwrite(&Color2, sizeof(int), 1, boardFile);
      fwrite(&CharNum, sizeof(int), 1, boardFile);
      fwrite(&Param1, sizeof(int), 1, boardFile);
      fwrite(&Param2, sizeof(int), 1, boardFile);
}

BoardTile is of BoardData. All these member variables of BoardData are int. [0][0] works fine, but [1][0] messes up the program.
0
 

Author Comment

by:SonicX_BlueBlur
ID: 12288006
Oh, by the way, BoardTile is a pointer to the templated linked list. It's inside another class (which is a templated linke list), and gets dynamically created when the class is constructed.
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 17

Expert Comment

by:rstaveley
ID: 12289962
> BoardTile is a pointer to the templated linked list

I'm only familiar with Axter's 2D array, which was suggested by your title. In my experience, it's not common practice to use indexing in linked lists... and 2D indexing looks particularly tricky. If you are talking about a dynamic_2d_array<BoardData*>  (as in a http://axter.com/code/dynamic_2d_array.h), can you show us the code in which you populate the array?
0
 

Author Comment

by:SonicX_BlueBlur
ID: 12290353
It is Axter's code. In the class containing this array is:

(public:) dynamic_2d_array<BoardData> *BoardTile;

The constructor for the top class:
BoardInfo::BoardInfo(char *nBoardName, int nWidth, int nHeight)
{
      BoardName = new char[strlen(nBoardName) + 1];
      strcpy(BoardName, nBoardName);
      bWidth = nWidth;
      bHeight = nHeight;

      BoardTile = new dynamic_2d_array<BoardData> (bWidth, bHeight);
}

(+20 pts)
0
 
LVL 17

Assisted Solution

by:rstaveley
rstaveley earned 25 total points
ID: 12290417
Since it is dynamic_2d_array<BoardData> rather than dynamic_2d_array<BoardData*>, I think you want to change...

    BoardTile[X][Y]->WriteData(boardFile);

...to...

    BoardTile[X][Y].WriteData(boardFile);

...but the sun is over the yard arm and I'm not at my most reliable right now %-}
0
 

Author Comment

by:SonicX_BlueBlur
ID: 12292652
Thing is, this:
dynamic_2d_array<BoardData> *BoardTile;
makes BoardTile a pointer, so I have to use ->. However, I see how you're declaring it... Should it be BoardData* in there instead of just BoardData? I'll try it.
0
 

Author Comment

by:SonicX_BlueBlur
ID: 12292780
No, that doesn't work. If I type:
dynamic_2d_array<BoardData*> *BoardTile;
the program won't compile anymore.
0
 
LVL 30

Expert Comment

by:Axter
ID: 12294887
The problem is that you're using a pointer instead of a static variable.

If you want to correctly access the operator[] functions, you need to dereference the pointer.
Example:
dynamic_2d_array<BoardData> *BoardTile = new dynamic_2d_array<BoardData> (bWidth, bHeight);

//Use ( ) to dereference variable
(*BoardTile)[1][0] = somevalue;

0
 
LVL 30

Expert Comment

by:Axter
ID: 12294896
You could also call the operators explicitly via following method:
BoardTile->operator[](1)[0] = somevalue;

The main problem is that you're using a pointer.  You would run into this problem when ever you try to access the operator[] from an object via a pointer.
0
 
LVL 30

Accepted Solution

by:
Axter earned 70 total points
ID: 12294910
I recommend that you make BoardTile member variable a static variable instead of a pointer.

You can still initialize it via initialize list.
BoardInfo::BoardInfo(char *nBoardName, int nWidth, int nHeight)
:BoardTile(nWidth, nHeight)
{
     BoardName = new char[strlen(nBoardName) + 1];
     strcpy(BoardName, nBoardName);
     bWidth = nWidth;
     bHeight = nHeight;
}

Notice the FULL colon on the initialize list line.
With the above example BoardTile is of the following type:
dynamic_2d_array<BoardData> BoardTile;
0
 

Author Comment

by:SonicX_BlueBlur
ID: 12296968
Ah. Well, thanks. The second method didn't work; the first and third did. I decided to go with the third.
rstaveley: You tried. You get points too :)
0
 
LVL 30

Expert Comment

by:Axter
ID: 12298464
>>I decided to go with the third.

IMHO, that's the better choice.
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question