Briefly, I have a template class Tree which has a createTree(Data* arrayData[]) method. This method receives an array of pointers to data of type Data :
template<class Data>
void Tree<Data>::createTree(Data* arrayData[]) {
m_indexCreation = 0;
cout << sizeof(arrayData) / sizeof(int) << endl;
m_root = createChildTree(arrayData);
}
In my main() function, I create an array of pointers to string objects and fill it. I then try to pass it to the createTree() method, but inside the method the array becomes of size 1. In other words, the count inside the method prints "1".
Here's my main() :
int main() {
string* tab[20];
for ( int i = 0 ; i < 20 ; i++ ) {
if ( i % 3 != 0 ) {
tab[i] = new string("James Johnson");
} else {
tab[i] = NULL;
}
}
Tree<string> tree;
tree.createTree(tab);
tree.display(tree.getRoot());
}
I can print without problem the values of the whole array in the main(), but in createTree(), the array somehow reduces to a size of 1, with single value of NULL.
What am I doing wrong?
sizeof(tabDonnees) / sizeof(int)probably produces 1. You're dividing the size of a pointer by the size of an int. Also, the method creerArbre (createTree) cannot determine the size oftab, it is only passed in by address, and it does not store its size, nor does the compiler pass its size.