• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

how to delcare dynamic array of objects?

PookyBoy

Senior member
#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.
 
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.
 
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
 
Back
Top