Pointer array
I seem to be getting a fatal error when trying to use the new operator on a member of a member array of pointers, like this:
class SOMECLASS
{
public:
SOMECLASS *array[8];
void SomeFunction()
{
array[0]=new SOMECLASS;
}
};
any idea why?
-------------------------------NeXe: NeHe DirectX-style. Follow the orange rabbit.
your not able to do DATA var[][8], only DATA var[8][]
If all these errors are so fatal, why am I still alive?
If all these errors are so fatal, why am I still alive?
If all these errors are so fatal, why am I still alive?
To clarify socks
you must do
array[0] = new SOMECLASS;
array[1] = new SOMECLASS;
array[2] = new SOMECLASS;
array[3] = new SOMECLASS;
array[4] = new SOMECLASS;
array[5] = new SOMECLASS;
array[6] = new SOMECLASS;
array[7] = new SOMECLASS;
to properly initialize the new objects
Dark
you must do
array[0] = new SOMECLASS;
array[1] = new SOMECLASS;
array[2] = new SOMECLASS;
array[3] = new SOMECLASS;
array[4] = new SOMECLASS;
array[5] = new SOMECLASS;
array[6] = new SOMECLASS;
array[7] = new SOMECLASS;
to properly initialize the new objects
Dark
ICQ: 130925152Email: e.j.folkertsma@student.utwente.nl
Dark -> I don''t necessarily want to make all of them new nodes, I might only want to make one of them with new.
-------------------------------NeXe: NeHe DirectX-style. Follow the orange rabbit.
It must be something else in your program (loop bounds?), because this code works just fine:
BTW, if you are creating some kind of list/tree data container you might want to check out STL, which will work with just about any class you can code.
- null_pointer
Sabre Multimedia
class SOMECLASS
{
public:
SOMECLASS *array[8];
void SomeFunction()
{
array[0]=new SOMECLASS;
delete array[0];
}
};
int main(int argc, char* argv[])
{
SOMECLASS object;
object.SomeFunction();
}
BTW, if you are creating some kind of list/tree data container you might want to check out STL, which will work with just about any class you can code.
- null_pointer
Sabre Multimedia
Here, I'll just post my entire tree code. It's for my AI that's supposed to think forward for a project in school.
Edited by - SeanHowe on July 27, 2000 6:49:26 PM
Edited by - SeanHowe on July 27, 2000 9:10:52 PM
class AINode{public: AINNode **branches; AINode *parent; AINode() { int i; branches=new AINode[BOARD_WIDTH]; for (i=0; i<BOARD_WIDTH; i++) { branches<i>=NULL; } parent=NULL; } ~AINode() { delete [] branches; } void DeleteChildren() { int i; for (i=0; i<BOARD_WIDTH; i++) { if (branches !=NULL) { branches<i>->DeleteChildren(); delete branches[i]; } } } int EvaluateMove(char b[BOARD_WIDTH*BOARD_HEIGHT]) { char bo[BOARD_WIDTH*BOARD_HEIGHT]; int i, j; int bestRet=99999, curRet, bestCol; ROWOF4 ro4; memcpy(bo, b, BOARD_WIDTH*BOARD_HEIGHT); for (i=0; i<BOARD_WIDTH; i++) { for (j=BOARD_HEIGHT-1; j>0; j--) { if (b[i+(j-1)*BOARD_WIDTH]!=' ') break; } if (j!=0) { bo[i+j*BOARD_WIDTH]=AI_CHARACTER; ro4=IsWin(bo, AI_CHARACTER); if (ro4.x1!=0 || ro4.x2!=0 || ro4.y1!=0 || ro4.y2!=0) { return 1; } branches[i]=new AINode; branches[i]->parent=this; } } for (i=0; i<BOARD_WIDTH; i++) { for (j=BOARD_HEIGHT-1; j>0; j--) { if (b[i+(j-1)*BOARD_WIDTH]!=' ') break; } if (j!=0) { curRet=branches[i]->EvaluateMove(bo); if (curRet<bestRet) { bestRet=curRet; bestCol=i; } } } if (parent!=NULL) return bestRet; else return bestCol; }};class AITree{public: AINode *start; void Init() { start=NULL; } void Release() { if (start==NULL) return; start->DeleteChildren(); delete start; start=NULL; } int ChooseColumn(char *b) { return start->EvaluateMove(b); }};
Edited by - SeanHowe on July 27, 2000 6:49:26 PM
Edited by - SeanHowe on July 27, 2000 9:10:52 PM
-------------------------------NeXe: NeHe DirectX-style. Follow the orange rabbit.
Hmmm... I haven''t tried this or anything, but here is some stuff that I see:
I would also recommend calling DeleteChildren from within the constructor, instead of relying on the using class, but that''s just opinion.
...Syzygy
... AINNode **branches; AINode *parent; AINode() { int i; // You want an array of POINTERS here branches=new AINode*[BOARD_WIDTH]; for (i=0; i<BOARD_WIDTH; i++) { branches<i>=NULL; } parent=NULL; } ~AINode() { delete [] branches; } void DeleteChildren() { int i; for (i=0; i<BOARD_WIDTH; i++) { // You want to check branches[i] here if (branches[i] != NULL) { branches[i]->DeleteChildren(); delete branches[i]; } } } ...
I would also recommend calling DeleteChildren from within the constructor, instead of relying on the using class, but that''s just opinion.
...Syzygy
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement