I am having a problem because my data is not able to access my private member functions even though i have multiple friend functions and i don't know what i am doing wrong.

/*Header File for Binary Tree

  April 2008

  Language: C++ (Microsoft Visual Studio 2008)

*/



#ifndef BINARYTREE_H_

#define BINARYTREE_H_



#include <iostream>

#include <string>

using namespace std;



namespace desulme

{ 

	// forward declarations

	template<typename T> class Node;



	template<typename T> void insertSorted(Node<T> *x, T d);

	template<typename T> void inOrderTraversal(Node<T> *x);

	template<typename T> void preOrderTraversal(Node<T> *x);

	template<typename T> void postOrderTraversal(Node<T> *x);

	template<typename T> T sum(Node<T> *x);

	template<typename T> int countLevels(Node<T> *x);

	template<typename T> int countNodes(Node<T> *x);



	

    template <typename T>

    class Node

	{      

	 private: Node<T> * left;

		  Node<T> * right;

		  T data;



	 public:  Node() {left=right=NULL;}

		  Node( T d) {left=right=NULL; data = d;}

		  ~Node();

		  friend void insertSorted(Node<T> *x, T d);

		  friend void inOrderTraversal(Node<T> *x);

		  friend void preOrderTraversal(Node<T> *x);

		  friend void postOrderTraversal(Node<T> *x);

		  friend T sum(Node<T> *x);

		  friend int countLevels(Node<T> *x);

		  friend int countNodes(Node<T> *x);

	};

    



	template<typename T>

    void insertSorted(Node<T> *x, T d)

	{	

		if(x==NULL)

		{

			x=new Node<T>(d);

	        }



		else if (d<=(x->data)) insertSorted<T>(x->left, d);

		else insertSorted<T>(x->right, d);

	}





    template<typename T>

    void inOrderTraversal(Node<T> *x)

	{	

		if (x == NULL) return; 

		else {

			  inOrderTraversal( x->left);

			  cout<<" "<< x->data;

			  inOrderTraversal( x->right);

		     }

	}





    template<typename T>

    void preOrderTraversal(Node<T> *x)

	{	

		if (x == NULL) return; 

		else {

			  cout<<" "<< x->data;

			  preOrderTraversal( x->left);

			  preOrderTraversal( x->right);

		     }

	}





    template<typename T>

    void postOrderTraversal(Node<T> *x)

	{	

		if (x == NULL) return; 

		else {

			  postOrderTraversal( x->left);

			  postOrderTraversal( x->right);

			  cout<<" "<< x->data;

		     }

	}



	

    template<typename T>

    T sum(Node<T> *x)

	{	

		if ( x == NULL) return 0;

		else return   x->data

			        + sum<T>(x->left)

					+ sum<T>(x->right);

	}





    template<typename T>

    int countLevels(Node<T> *x)

	{   

		if ( x == NULL) return 0;

	    else {int l = countLevels<T> (x->left);

		      int r = countLevels<T> (x->right);

			  int max = l<r?r:l;		

		      }

          	  return 1 + max;

	}





    template<typename T>

    int countNodes(Node<T> *x)

	{	

		if ( x == NULL) return 0;



		else return 1 + countNodes(x->left) 

			          + countNodes(x->right);

	}

}



#endif /*BINARYTREE_H_*/
#include <iostream>

#include "binarytree.h"

using namespace std;

using namespace desulme;



int main()

{

	cout << "This is a Binary Tree of Integers" << endl << endl;

	Node<int> * x = new Node<int> (5);  //creates starting node

	insertSorted<int>(x, 8);

	insertSorted<int>(x, 2);

	insertSorted<int>(x, 23);

	insertSorted<int>(x, 4);

	insertSorted<int>(x, 1);

	insertSorted<int>(x, 12);

	insertSorted<int>(x, 16);

	insertSorted<int>(x, 2);

	insertSorted<int>(x, 20);



	inOrderTraversal<int> (x);

	cout << endl << endl;



	preOrderTraversal<int> (x);

	cout << endl << endl;



	postOrderTraversal<int> (x);

	cout << endl << endl;

			  

	cout << "The sum is " << sum<int>(x) << endl << endl;

	

	cout << "The count of levels is " << 1 + countLevels<int>(x)<< endl << endl;



	cout << "The count of nodes is " << 1 + countNodes<int>(x) << endl << endl;



}

It's simple, the friend declarations don't match your function declarations. However, you're hiding that particular error by assuming that using the class' version of T automagically makes the friend declarations template functions. It doesn't, you have to specify that yourself. For example:

friend void insertSorted(Node<T> *x, T d);

becomes:

template <typename U> friend void insertSorted(Node<U> *x, U d);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.