Solved

(Axter) Your 2D Array Code

Posted on 2004-10-12
13
225 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

743 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now