-1

I'm working on adding fruit into a binary subtree for school and I wrote a program and these are the compilation errors I'm getting. I cannot see any glaring syntax errors and I know it helps to have other sets of eyes inspect the code as well.

compilation error

//BST.CPP//
#include <iostream>
#include <cstdlib>
#include "BST.h"

using namespace std;





BST::BST(){
    root = NULL;

}
BST::node* BST::CreateLeaf(int key, string name){

    node* n = new node;
    n->key = key;
    n->name = name;
    n->left = NULL;
    n->right = NULL;

    return n;
}

void BST::AddLeaf(int key, string name){
    AddLeafPrivate(key, name, root);
}

void BST::AddLeafPrivate(int key, string name, node* ptr){
    if(root == NULL){

        root = CreateLeaf(key, name);
    }
    else if(key < ptr->key)
    {
        if(ptr->left != NULL){

            AddLeafPrivate(key, name, ptr->left);
        }
        else{

            ptr->left = CreateLeaf(key, name);
        }
    }
    else if(key > ptr->key)
    {

        if(ptr->right != NULL){

            AddLeafPrivate(key, name, ptr->right);
        }
        else{

            ptr->right = CreateLeaf(key, name);
        }
    }
    else{
        cout << "The fruit"<< name<<" has alreadt been added \n";
    }
}

void BST::PrintInOrder(){
    PrintInOrderPrivate(root);
}

void BST::PrintInOrderPrivate(node* ptr){

    if(root!= NULL){
        if(ptr->left != NULL){
            PrintInOrderPrivate(ptr->left);
        }
        cout<< ptr->key<< " ";
        cout << ptr->name<< " ";
        if(ptr->right != NULL){
            PrintInOrderPrivate(ptr->right);
        }
    }
    else{
        cout<<"The tree is empty\n";
    }

}



 //BST.h//
 #include <string>

using namespace std;

class BST{
private:

struct node{
    int key;
    string name;
    node* left;
    node* right;
};
    node* root;

    void AddLeafPrivate(int key, string name, node* ptr);
    void PrintInOrderPrivate(node* ptr);
public:

    BST();
    node* CreateLeaf(int key, string name);
    void AddLeaf(int key, string name);
    void PrintInOrder();
};


//main.cpp//
#include <iostream>
#include <cstdlib>
#include "BST23.cpp"

using namespace std;

int main(){

    int treekeys[7] = {1 , 2, 3, 4, 5, 6 ,7};
    string treenames[7] = {"blueberry", "peach", "apricot", "pear", "cherry", "mango", "papaya"};

    BST tree;

    cout << "Tree before adding any values:\n";

    tree.PrintInOrder();

    for(int i=0; i < 7; i++){
        tree.AddLeaf(treekeys[i], treenames[i]);
    }

    cout<<"Printing tree in order";

    tree.PrintInOrder();

    return 0;
    }
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189

1 Answers1

0

In short: In main.cpp you need to include the bst header file instead of the cpp file.

I am guessing that you (or your IDE) compile both main.cpp and BST23.cpp and link them.

If you include BST23.cpp in main.cpp, then there will be out-of-class definitions of all of BST member functions in both translation units, which will result in a link-time error because it is a violation of one definition rule

You only need the declarations in main.cpp, that's why you only need to include the header file.

sparik
  • 1,181
  • 9
  • 16