My problem is that i get this error
binarytree.cpp: In member function ‘void BinaryTree<T>::printPaths(const BinaryTree<T>::Node*) const [with T = int]’:
binarytree.cpp:88: instantiated from ‘void BinaryTree<T>::printPaths() const [with T = int]’
main.cpp:113: instantiated from ‘void printTreeInfo(const BinaryTree<T>&, const std::string&, const std::string&) [with T = int]’
main.cpp:47: instantiated from here
binarytree.cpp:116: error: passing ‘const BinaryTree<int>’ as ‘this’ argument of ‘void BinaryTree<T>::findPaths(BinaryTree<T>::Node*, int*, int) [with T = int]’ discards qualifiers
compilation terminated due to -Wfatal-errors.
I understand that it could be the template that is causing scope issues I dont want it to think that Node member variable of the BinaryTree class How do I accomplish this?
// printPaths()
template <typename T>
void BinaryTree<T>::printPaths() const
{
printPaths(root);
}
template <typename T>
void BinaryTree<T>::printPath(int path[],int n)
{
for(int i = 0; i<n; i++)
cout << (char)path[i] << " ";
cout << endl;
}
template<typename T>
void BinaryTree<T>::findPaths(Node * subroot, int path[], int pathLength)
{
if(subroot == NULL) return;
path[pathLength] = subroot->elem;
pathLength++;
if(subroot->left == NULL && subroot->right = NULL)
printPath(path,pathLength);
else
{
findPaths(subroot->left, path, pathLength);
findPaths(subroot->right,path,pathLength);
}
}
template<typename T>
void BinaryTree<T>::printPaths(const Node* subroot) const
{
int path[100];
findPaths(subroot,path,0);
}
The problem is that you are calling
findPaths()(non-constmember) fromprintPaths()(constmember) function. Calling non-const member from const member is not allowed by C++.You have to re-write the code by either making both
printPaths()as non-const methods orfindPaths()andprintPath()asconstmethods.