-2

I am doing some coding part to find the path from root to all leaf but it shows error message. Here i am try to build n-ary tree and try to print all the paths from root to leaf node. So for this here i am trying to implement some functions and when i call the function it shows error. When i run the code it shows error :

main.cpp: In function 'void printAllRootToLeafPath(TreeNode)':
main.cpp:33:39: error: too many arguments to function 'void printAllRootToLeafPath(TreeNode)'
33 | printAllRootToLeafPath(t , vec);
| ^
main.cpp:27:8: note: declared here
27 | void printAllRootToLeafPath(struct TreeNode t)

#include <iostream>
#include <vector>
#include<cstring>
#include <string>
#include <sstream>

using namespace std;
int m = 0;

struct TreeNode
{
    int ownAddress ;
    int parentAddress;
    vector< int > childrenAddress ;

} info[10] ;

 void printPath(vector<int> vec)
    {
         // Print elements in the vector
            for (int ele : vec) 
            {
            cout << ele << " ";
            }
            cout << endl;
    }
  void printAllRootToLeafPath(struct TreeNode t)
            
    {
        
        vector<int> vec;
        
        printAllRootToLeafPath(t , vec);
    }
    
   void printAllRootToLeafPath(struct TreeNode root , vector<int>vec)
   
    {
                ostringstream str1 , str3 ;
                str1 << root.ownAddress;
                str3 << root.childrenAddress[0];
                string var1 = str1.str();
                string var3 = str3.str();
                if(var1.compare(var3) == 0)
                {                   
                      // Print the path
                        printPath(vec);
 
                    // Pop the leaf node and return 
                        vec.pop_back();
                        return;                                         
                }
                // Recur for all children of the current node
                int child = sizeof(root.childrenAddress)/sizeof(root.childrenAddress[0]);
                for (int i = 0;i < child; i++)
                      // Recursive Function Call
                        printAllRootToLeafPath(info[i], vec);
    }
    
    int main()
    
    {
    
    info[0].ownAddress = 478523;
    info[0].parentAddress = 478523;
    info[0].childrenAddress.push_back (124569);
    info[0].childrenAddress.push_back (253946);
    info[0].childrenAddress.push_back (325489);
    
    
    // Node 1 Relation who is the parent and child of that node.
    info[1].ownAddress = 124569;
    info[1].parentAddress = 478523;
    info[1].childrenAddress.push_back (423657);
    info[1].childrenAddress.push_back (523498);
    
    // Node 2 Relation who is the parent and child of that node.
    info[2].ownAddress = 253946;
    info[2].parentAddress = 478523;
    info[2].childrenAddress.push_back (632549);
    
    // Node 3 Relation who is the parent and child of that node.
    info[3].ownAddress = 325489;
    info[3].parentAddress = 478523;
    info[3].childrenAddress.push_back (745863);
    
    // Node 4 Relation who is the parent and child of that node.
    info[4].ownAddress = 423657;
    info[4].parentAddress = 124569;
    info[4].childrenAddress.push_back (852369);
    
    // Node 5 Relation who is the parent and child of that node.
    info[5].ownAddress = 523498;
    info[5].parentAddress = 124569;
    info[5].childrenAddress.push_back (963258);
    
    // Node 6 Relation who is the parent and child of that node.
    info[6].ownAddress = 632549;
    info[6].parentAddress = 253946;
    info[6].childrenAddress.push_back (102359);
    
    // Node 7 Relation who is the parent and child of that node.
    info[7].ownAddress = 745863;
    info[7].parentAddress = 325489;
    info[7].childrenAddress.push_back (745863);
    
    // Node 8 Relation who is the parent and child of that node.
    info[8].ownAddress = 852369;
    info[8].parentAddress = 423657;
    info[8].childrenAddress.push_back (852369);

    
    // Node 9 Relation who is the parent and child of that node.
    info[9].ownAddress = 963258;
    info[9].parentAddress = 523498;
    info[9].childrenAddress.push_back (963258);
    
    // Node 10 Relation who is the parent and child of that node.
    info[10].ownAddress = 102359;
    info[10].parentAddress = 632549;
    info[10].childrenAddress.push_back (102359);
    
    
    printAllRootToLeafPath(info[m]);
    return 0;
    };
    
    
    
Filburt
  • 17,626
  • 12
  • 64
  • 115

2 Answers2

2

Declaration order matters!

When the one-argument printAllRootToLeafPath function tries to do the call

printAllRootToLeafPath(t , vec);

the compiler doesn't know about the two-argument function yet.

Simple solution: Change the order of the two printAllRootToLeafPath functions.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

The free function

void printAllRootToLeafPath(struct TreeNode t)

is calling

void printAllRootToLeafPath(struct TreeNode root , vector<int>vec)

which means the it must have been declared first.

Swap order of the functions:

void printAllRootToLeafPath(struct TreeNode root , vector<int>vec) {
    // ...
}
void printAllRootToLeafPath(struct TreeNode t) {
    // ...
}

...or you could instead forward declare the version that takes two arguments:

// forward declaration
void printAllRootToLeafPath(struct TreeNode root , vector<int>vec);

void printAllRootToLeafPath(struct TreeNode t) {
    // ...
}

// definition
void printAllRootToLeafPath(struct TreeNode root , vector<int>vec) {
    // ...
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108