0

I have problems with a class method returning all paths of binary tree. My compiler (Visual Studio) doesn't allow recursion in this instance; Additionally I get error C2440, apparently for trying to convert <lambda_276bde719f5b574aaaebc799da75f887>" to "int" (?)

string* tree::binaryCoding(string list) {
    string* binaryList = new string[list.length()];
    node *temporaryNode=trunk;
    auto h = [=](node* current, string position) -> void{
        if (current->getCharacter() != NULL) {
            int i = 0;
            while (binaryList[i] != "") ++i;
            binaryList[i] = current->getCharacter() + position;
            return;
        };
        h(current->getOne(), position + "1");
        h(current->getZero(), position + "0");
    };
    h(trunk, "");
    return binaryList;
}

I've tried making two lambdas identical in functionality that called each other, but the first one couldn't use the second one. I want to know if what I'm doing is valid or if I should take a different approach.

Stachowe
  • 1
  • 2
  • You capture `h` by copy... – Jarod42 Apr 15 '23 at 15:33
  • C++ scope rules work such that `h` is not fully defined in the body of the lambda, so you can't implement a recursive lambda this way. In C++23 you will be able to do via "deducing this". In C++11 through C++20, you can do it via defining an `std::function` variable with the right signature first then assigning the var to the lambda and letting the lambda capture the variable by reference. – jwezorek Apr 15 '23 at 15:42

0 Answers0