简体   繁体   中英

How to pass a dynamic 2d array of strings as a parameter in C++

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 << "*" << " ";
    }
}
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]; //...
  }
}
 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.

the method call is given by

printTree(tree [3][maxNumberOfNodes]);

it's working for me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM