LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-08-2003, 12:15 AM   #1
PTBmilo
Member
 
Registered: Jan 2003
Posts: 167

Rep: Reputation: 30
dynamic array in class help


Hi all. I have written a program for the LIFE game. I would like to make it possible for the user to change the world size, but am having some trouble making it work from within a class. I'm pretty sure that the problem is in this code:

Maybe somebody can see what I'm doing wrong?
Code:
class lifeBoard
{
public:
        void setEnv(int RCOUNT, int CCOUNT);
        typedef int* IntPoint;
private:        
        IntPoint *board;
        IntPoint *neighborCount;
}

void lifeBoard::setEnv(int RCOUNT, int CCOUNT)
{
        board=new IntPoint[RCOUNT];
        neighborCount=new IntPoint[RCOUNT];
        for(int row=0;row<RCOUNT;row++)
        {
                board[row]=new int[CCOUNT];
                neighborCount[row]=new int[CCOUNT];
        }
}
 
Old 03-08-2003, 12:32 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
I'll keep looking at the code some more, but I don't see anything immediately.

I do have a suggestion though. Is there any particular reason you want an array of arrays of ints? Why not do this:

Code:
class lifeBoard
{
public:
        void setEnv(int RCOUNT, int CCOUNT);
        // typedef int* IntPoint;
private:        
        int *board;
        int *neighborCount;
}

void lifeBoard::setEnv(int RCOUNT, int CCOUNT)
{
        board=new int[RCOUNT * CCOUNT];
        neighborCount=new int[RCOUNT * CCOUNT];
        // Initialization here for good measure...
        for(int index=0; index<(RCOUNT * CCOUNT); index++)
        {
                board[index]=0;
                neighborCount[index]=0;
        }
}
From that point on, you just need to be consistent in handling your rows and columns. As in row 2, column 4 (assuming 0 base) could be identified in one of two ways:
1) board[2*RCOUNT + 4]
2) board[2 + 4*CCOUNT]

The fewer nested pointers the better...
 
Old 03-08-2003, 01:23 PM   #3
PTBmilo
Member
 
Registered: Jan 2003
Posts: 167

Original Poster
Rep: Reputation: 30
Thanks for looking at the code. It was helpfull knowing that I needed to look elsewhere for the error. I found it this morning.... I needed CCOUNT and RCOUNT in later calculations, so I threw them in the class and all was O.K.:
(relevant code
Code:
class lifeBoard
{
public:

        int RCOUNT;
        int CCOUNT;
        void setEnv(int RCOUNT, int CCOUNT);
        void output();
        typedef int* IntPoint;
private:
        IntPoint *board;
        IntPoint *neighborCount;
};

void lifeBoard::setEnv(int RCOUNT, int CCOUNT)
{
        this->RCOUNT=RCOUNT;
        this->CCOUNT=CCOUNT;
        board=new IntPoint[RCOUNT];
        neighborCount=new IntPoint[RCOUNT];
        for(int row=0;row<RCOUNT;row++)
        {
                board[row]=new int[CCOUNT];
                neighborCount[row]=new int[CCOUNT];
        }
}

void lifeBoard::setCleanBoard()
{
        for(int row=0;row<RCOUNT;row++)
        {  
                for(int colum=0;colum<CCOUNT;colum++)
                {
                        board[row][colum]=0;
                        neighborCount[row][colum]=0;
                }
        }       
}
At least, this is how I got it to work, I don't know if it's the 'accepted' way of doing it (I'm curious about the "this->"... should I use a different method?)

Your suggestion about the array is interesting. I am going to play around with that.

Thanks again.
 
Old 03-08-2003, 03:56 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Whichever version you understand better is the better way to go. You have enough to deal with in developing new code rather than trying to decipher some obscure way of making data structures.

I would suggest changing the variable names for RCOUNT and CCOUNT; either in the class itself or the setEnv() method. Personally, for those types of methods, I usually name them "new_<var name>". Like new_RCOUNT and new_CCOUNT. That way, you could simply say "RCOUNT = new_RCOUNT" and "CCOUNT = new_CCOUNT". That would also get rid of the "this->" references and it should be completely clear what's going on to anybody looking at the code.

Again, what you're comfortable with and understand is the most important thing. What you showed in your last post should work fine. Just remember to free/delete your board and neighborCount pointers in your class destructor... otherwise you'll have a big, fat memory leak...
 
Old 03-08-2003, 06:26 PM   #5
PTBmilo
Member
 
Registered: Jan 2003
Posts: 167

Original Poster
Rep: Reputation: 30
Thank you. I had it all working for a while, then I decided to include a 'resetEnv' member. Something wasn't working and now I'm way off on a tangent. Suppose I have this:

Code:
class lifeBoard
{
public:
        typedef int* IntPoint;
private:
        struct environment
        {
                int RCOUNT;
                int CCOUNT;
                IntPoint *board;
                IntPoint *neighborCount;
        };

        environment* world;
};
Then have a default constructor with, say:
Code:
lifeBoard::lifeBoard(int newRCOUNT,int newCCOUNT)
{
        setEnv(newRCOUNT,newCCOUNT);
}

void lifeBoard::setEnv(int newRCOUNT, int newCCOUNT)
{
        world=new environment;

        world->RCOUNT=newRCOUNT;
        world->CCOUNT=newCCOUNT;
        world->board=new IntPoint[newRCOUNT];
        world->neighborCount=new IntPoint[newRCOUNT];
        for(int row=0;row<newRCOUNT;row++)
        {
                world->board[row]=new int[newCCOUNT];
                world->neighborCount[row]=new int[newCCOUNT];
        }
        setCleanBoard();
}
If I ever want to reference *world.RCOUNT, I use this:

Code:
void lifeBoard::resetEnv(int newRCOUNT, int newCCOUNT)
{
        for(int row=0, row<(world->RCOUNT),row++)
        {
                delete [] world->board[row];
                delete [] world->neighborCount[row];
        }

        delete [] world->board;
        delete [] world->neighborCount;

        setEnv(newRCOUNT,newCCOUNT);
}
But the compiler spits out this:
Code:
/home/milo/ENG_38/dev [222]> g++ lifeClass-point.cpp
lifeClass-point.cpp: In method `void lifeBoard::resetEnv(int, int)':
lifeClass-point.cpp:59: parse error before `)'
lifeClass-point.cpp:62: void value not ignored as it ought to be
lifeClass-point.cpp:63: parse error before `}'
This is the first function after the class dec, and I'm sure it'd continue on the next one if I moved it.

I know that this is a total overkill for the problem, I am just trying to learn the tools (understand pointers?)

Can you see where I'm going wrong?

Last edited by PTBmilo; 03-08-2003 at 06:27 PM.
 
Old 03-08-2003, 07:27 PM   #6
rmartine
Member
 
Registered: Dec 2002
Location: San Luis Obispo, CA
Distribution: Fedora Core 3
Posts: 618

Rep: Reputation: 30
When you see a parse error you should start looking for "syntax-type" errors before where the compiler tells you the error is.

From looking at the resetEnv function you have

for(int row=0, row<(world->RCOUNT),row++)

I think you want semicolons instead of commas in that for loop.

My 2 cents. BTW I like your new way of making the lifeboard class. It was easier for me to understand.

Again my 2 cents.
 
Old 03-09-2003, 02:35 AM   #7
PTBmilo
Member
 
Registered: Jan 2003
Posts: 167

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by rmartine

I think you want semicolons instead of commas in that for loop.
boy do I feel silly

That was it man. Thank you very much.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamic class loading doesn't find my functions!? Thinking Programming 1 07-28-2005 02:12 PM
dynamic class instantiation in c++? Thinking Programming 3 07-28-2005 08:59 AM
Array declaration in class or main function??? redhatrosh Programming 4 03-15-2005 02:13 PM
Dynamic Class Loading Problems dmnc Programming 11 07-22-2004 04:06 PM
Java help (accessing array elemonts from another class or method) Tru_Messiah Programming 6 05-14-2004 09:20 AM


All times are GMT -5. The time now is 06:38 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration