how to delcare dynamic array of objects?

PookyBoy

Senior member
Aug 18, 2001
200
0
0
#include <string.h>
#include "BTreeNodeValue.h"

class BTreeNode {
public:
BTreeNodeValue *values;
BTreeNode *childrens;

// Constructor
BTreeNode(int order) {
//values = (BTreeNodeValue *) malloc (order*sizeof(BTreeNodeValue));
values = new BTreeNodeValue[order];
//childrens = (BTreeNode *) malloc ( (order+1)*sizeof(BTreeNode) );
childrens = new BTreeNode[order+1];

// initialize to null just in case
int i;
for (i=0; i<order; i++)
values = NULL;
for (i=0; i<(order+1); i++)
childrens = NULL;
}
};

I get all these "no matching function for call to `BTreeNodeValue::BTreeNodeValue..." errors when complied with g++ on unix. I tried it with the malloc method and still get errors. How am I suppose to do it? Thanks.
Hm I copy and pasted the code dunno why there's no indentation.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
You didn't show your test code, or the whole error message, so to some degree I'm guessing.

Are you sure you're calling the constructor correctly? ie. with an int argument?
Should look something like this:

BTreeNode x(5);

If you call it without an argument, you will get an error similar to what you described.
 

Adrian Tung

Golden Member
Oct 10, 1999
1,370
1
0
Firstly, the compiler seems to be complaining about BTreeNodeValue, so I think the problem lies somewhere there. How did you declare your constructor for BTreeNodeValue? In order to use new, or malloc for that matter, I think you'll need a default constructor for BTreeNodeValue that doesn't receive any variables.

Secondly, do *not* initialize your pointers to NULL *after* allocating memory to them, as your for loops do. You'll invalidate all your pointers and end up with memory leaks.


Hope that helps,
:)atwl