?
Solved

(Axter) Your 2D Array Code

Posted on 2004-10-12
13
Medium Priority
?
242 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 100 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 280 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.
Suggested Courses

718 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