Solved

(Axter) Your 2D Array Code

Posted on 2004-10-12
13
238 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
Independent Software Vendors: 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

626 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