I am trying to implement a binary tree as a 2d array. Now I want to the user to enter the required height of the tree and the program gives an appropriate size array. I then 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>
using std::string;
using std::cout;
void printTree( const string* tree, size_t Rows, size_t Cols );
int main()
{
int treeHeight = 0;
int maxNumberOfNodes = 1;
std::cin >> treeHeight;
int maxNumberOfNodes = treeHeight*treeHeight - 1;
cout << maxNumberOfNodes;
if ( maxNumberOfNodes < 1 ) return 1;
string* tree = new string [ 3*maxNumberOfNodes ];
printTree( tree, 3, maxNumberOfNodes );
delete [] tree; // remember to clean up
}
void printTree( const string* tree, size_t Rows, size_t Cols ) {
for ( size_t row=0; row < Rows; ++row ) {
cout << "*" << " ";
}
}
EDIT:
Routine disclaimer: "Don't use new/delete in modern C++."
That is not quite true though. There are two cases, where explicit new in code is fine:
A. Teacher requires that in the homework.
B. When two stellar constellations do align. (A rare event.) The constellations are:
1. The clearest, most maintainable implementation is with new rather than modern C++ constructs
2. You truly master C++