Solved

(Axter) Your 2D Array Code

Posted on 2004-10-12
13
232 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

739 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