im trying to run this code that adds a name and displays it. i keep getting the same message. can anyone tell me what i have done wrong?
thank you.

#include <iostream>
#include <string>
using namespace std;

class treenode
{
      public:
             string data;
             treenode * left;
             treenode * right;
             

            // Constructor.  Make a node containing "student".  
           treenode(string student) 
           {
            data = student;
            left = NULL;
            right = NULL;
         }

};

class MyBST
{
private:      
     treenode * root;
    
   
public:
       
       MyBST();
        //Insert item s into a tree rooted at r
     void add(treenode * & r, string student);


	 //Display all items in the tree in order (in-order traversal)
     void printAlpha(treenode * r); 
     
    
};   


//constructor
MyBST::MyBST()
{
    root = NULL;          
}


//function to add
void add(treenode * & r, string s)
{
    if( r == NULL ) //empty tree, easy case!!!!!!
    {
        //create a new node, point r at it
        r = new treenode(s);    
    }
    else if( s > r->data ) //go right!
    {
        add( r->right, s );     
    }
    else  // go left
    {
        add( r->left, s);      
    }
}


//function to print in alphabetical order
void printAlpha(treenode * r)
{
    if( r != NULL )
    {
        printAlpha(r->left);  //print items in left subree
        cout << r->data << endl; //print item at root        
        printAlpha(r->right); //print items in right subtree   
    }     
     
}



//////////////
//main program
//////////////

int main()
{
    
  MyBST firsttry;
  firsttry.add("name");
  
  firsttry.display();
      
        
    while(true){}
    return 0;   
}

What message?

it states:
no matching function for call to 'MyBST::add[int, const char[5]]'
something along those lines.
im using Dev-C++ to complie and run this code.

That would be because you didn't associate your implementations for add() and printAlpha() to your class definition. You need to use the scope-resolution operator '::', just like you did with your MyBST constructor.

i added the scope-resolution operator, but i still get the same message as before.

You still get it for the same method?

Perhaps the main method needs to go to the top of the file? I could be wrong though, I have always seperated my classes into seperate files.

i added the scope-resolution operator, but i still get the same message as before.

I'm betting you did this:

void ::add(treenode * & r, string s)
{
/* ... */
}

Instead of this:

void MyBST::add(treenode * & r, string s)
{
/* ... */
}

There are other errors as well. Your firsttry variable are calling the methods wrongly.

int main()
{   

	MyBST firsttry;  
	firsttry.add("name");   

	firsttry.display();      
	
	
	while(true){}    
	return 0;   
}

The add function needs 2 parameters and there is no display function defined for MyBST class.

Here's a working piece of code:

// TreeNode.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <string>

using namespace std;

class treenode
{
	public:
		string data;
		treenode *left;
		treenode *right;

		treenode(string student)
		{
			data = student;
			left = NULL;
			right = NULL;
		}
};

class MyBST
{
	//private:
	//	treenode *root;

	public:
		treenode *root;
		MyBST();
		void add(treenode * & r, string student);
		void printAlpha(treenode * r);
};

MyBST::MyBST()
{
	root = NULL;
}

void MyBST::add(treenode * & r, string s)
{
	if(r == NULL)
	{
		r = new treenode(s);
	}
	else if(s > r->data)
	{
		add(r->right, s);
	}
	else
	{
		add(r->left, s);
	}
}

void MyBST::printAlpha(treenode *r)
{
	if(r != NULL)
	{
		printAlpha(r->left);
		cout << r->data << endl;
		printAlpha(r->right);
	}
}

int main(int argc, char* argv[])
{
	MyBST firsttry;
	firsttry.add(firsttry.root, "name");

	firsttry.printAlpha(firsttry.root);

	return 0;
}
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.