0

I am trying to implement a binary tree as a 2d array. I want the user to enter the required height of the tree and the program should give an appropriate size array. Then, I want to print the array, which is why I need to pass it as a parameter. However, I get the following error:

arrayTree/main.cpp|19|error: cannot convert ‘std::__cxx11::string** (*)[maxNumberOfNodes] {aka std::__cxx11::basic_string<char>** (*)[maxNumberOfNodes]}’ to ‘std::__cxx11::string** {aka std::__cxx11::basic_string<char>**}’ for argument ‘1’ to ‘void printTree(std::__cxx11::string*)’|

Please, what is causing the error and how can I fix it?

#include <iostream>
#include <string>
#include <math.h>

using namespace std;
void printTree(string** tree);
int main()
{
    int treeHeight = 0;
    int maxNumberOfNodes = 1;
    cout << "enter tree height";
    cin >> treeHeight;
    cout << treeHeight<< "\n";

    //create an array that can hold every combination for a given tree height
    maxNumberOfNodes = pow(2,treeHeight) - 1;
    string** tree [3][maxNumberOfNodes];
    cout << maxNumberOfNodes;
    printTree(tree);

}

 void printTree(string** tree){
//not fully implemented yet
    for(int i=0; i < sizeof(tree);  i++){
        cout << "*" << " ";
    }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
uhexos
  • 383
  • 4
  • 19

3 Answers3

3
string** tree [3][maxNumberOfNodes];

is the syntax of a static 2D array of type string** , where both dimensions have to be declared const.

The difference between a static and a dynamic array is shown in here: Multidimensional variable size array in C++

Instead you want to write something like

string** tree = new string*[3];
for(int i = 0; i < 3; i++)
   tree[i] = new string[maxNumberOfNodes];

As @Remy Lebeau commented: Every occurrence of new[] needs to be answered by a delete[] call, like this:

for (int i = 0; i < 3; i++)
    delete tree[i];
delete[] tree;

to remove the dynamic allocation from the heap.

Like @drescherjm pointed out sizeof(tree) is not valid, as tree is just a pointer and does not include size information about the array.

You could solve this by additionally passing the dimensions of your array with it:

void printTree (string** tree, int dim, int dim2)

and rewriting the loop to

for(int i = 0; i < dim; i++){
  for(int j = 0; j < dim2; j++){
    cout << tree[i][j]; //...
  }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Szilan
  • 83
  • 1
  • 1
  • 8
  • 1
    Don't forget to `delete[]` the arrays when you are done with them: `for(int i = 0; i < 3; i++) { delete[] tree[i]; } delete[] tree;` – Remy Lebeau Mar 20 '19 at 00:18
1
string** tree [3][maxNumberOfNodes];

This declares a 2D array of string** pointers. That is not what you want. You want a 2D array of string objects instead, so drop the pointers:

string tree [3][maxNumberOfNodes];

Also, your printTree() is not implemented correctly. It would need to be implemented more like this instead:

void printTree(string** tree, int height) {
    for(int i = 0; i < 3;  i++) {
        for(int j = 0; j < height; j++) {
            // use tree[i][j] as needed ...
        }
    }
}

That being said, since the value of maxNumberOfNodes is not known until runtime, the string tree [3][maxNumberOfNodes] syntax is declaring a Variable Length Array, which is not officially supported by the C++ standard, only as an extension by a few C++ compilers. You need to use new[] instead to allocate the 2nd dimension:

string* tree [3];
for(int i = 0; i < 3; ++i)
    tree[i] = new string[maxNumberOfNodes];

printTree(tree, maxNumberOfNodes);

for(int i = 0; i < 3; ++i)
    delete[] tree[i];

Or better, use std::vector instead:

std::vector<string> tree [3];
for(int i = 0; i < 3; ++i)
    tree[i].resize(maxNumberOfNodes);

Though, in this latter case, you won't be able to pass tree to a string** function parameter, so you will have to adjust the code accordingly.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • using void printTree(string** tree, int height) throws an error for me. I get the error: "cannot convert ‘int**’ to ‘int***’ for argument ‘1’ to ‘void printTree(int***, int)’ " however, printTree(string* tree, int height) works as expected – uhexos Mar 20 '19 at 00:45
  • @uhexos a tree of `string` values [works fine for me](https://ideone.com/qo3sey). If you are having errors with a separate tree of `int` values, then you are doing something wrong with it. Please edit your question to include that code. Looks like you need to go back to basics and review how pointers work in general. – Remy Lebeau Mar 20 '19 at 00:51
0

the method call is given by

printTree(tree [3][maxNumberOfNodes]);

it's working for me

Abinav R
  • 365
  • 2
  • 16
B.Dorsey
  • 11
  • 1
  • 5