0

I have one header file and two c++ files and when i try to compile my main file i get

> Undefined symbols for architecture arm64:      
>"BST::insert(BST::TreeNode*, int)", referenced from:
      BST::insert(int) in assignment3-ad4458.o
  "BST::inOrder(BST::TreeNode*)", referenced from:
      BST::inOrder() in assignment3-ad4458.o
  "BST::preOrder(BST::TreeNode*)", referenced from:
      BST::preOrder() in assignment3-ad4458.o
  "BST::postOrder(BST::TreeNode*)", referenced from:
      BST::postOrder() in assignment3-ad4458.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I've tried changing my compiler but no change. Also tried linking my main.cpp file to my other cpp file but still same error

BST.h file

class BST{

private:
        //default
        struct TreeNode{
        TreeNode(int value): val(value), left(NULL), right(NULL) {

        }
        int val;
        TreeNode *left;
        TreeNode *right;
        };

        TreeNode *root;
        void insert(TreeNode *t, int val);
        void inOrder(TreeNode * t);
        void preOrder(TreeNode * t);
        void postOrder(TreeNode * t);
public:
        // constructor          
        BST();     
        
        void insert(int val){ 
            insert(root, val);
        }       

        void inOrder() {
            inOrder(root);
        }
        void preOrder(){
            preOrder(root);
        }
        void postOrder(){
            postOrder(root);
        }
};

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

BST.cpp file

#include "BST.h"
#include <iostream>
#include <stddef.h>

using namespace std;


void BST::insert(TreeNode *t , int val)
{
        if (!t)
        {
            //insert new node if root is null
            t = new TreeNode(val);           
            root = t;           
        }
        else{
            //insert val
            if (val < t->val){
                
                if (!t->left){
                        TreeNode *treeTemp = new TreeNode(val);
                        t->left = treeTemp;
                    }
                    else
                        //process left node
                        insert(t->left, val);
                }
                else{
                    if (!t->right){
                        TreeNode *treeTemp = new TreeNode(val);                         
                        t->right = treeTemp;
                        }
                    else
                        //process right node
                         insert(t->right, val);
                }
        }
}



void BST::inOrder(TreeNode * t)
{
    if (!t)
        return;
    //recur left subtree
    inOrder(t->left);
    //read data of child
    cout << t->val << " " ;
    //recur right subtree
     inOrder(t->right);
}

void BST::preOrder(TreeNode * t)
{
        if (!t)
            return;
        //read data of child
        cout << t->val << " ";
        //reur on left subtree
        preOrder(t->left);
        //recur on right subtree
        preOrder(t->right);
}

void BST::postOrder(TreeNode * t)
{
        if (!t)
            return;
        //recur on left subtree
        postOrder(t->left);
        //recur on right subtree
        postOrder(t->right);
        //read data of child
        cout << t->val << " ";
}

Assignment.cpp file/main

#include "BST.h"
#include <iostream>


using namespace std;




int main()
{
        BST myBST;
        myBST.insert(10);
        myBST.insert(7);
        myBST.insert(29);
        myBST.insert(30);
        myBST.insert(9);
       
        cout<< "inOrder: ";       
        myBST.inOrder();   //the output should be: 7, 9, 10, 29, 30
        cout << "preOrder: ";
        myBST.preOrder();  //the output should be: 10, 7, 9, 29, 30
        cout << "postOrder: ";
        myBST.postOrder(); //the output should be: 9, 7, 30, 29, 10
        
}

clang version

Apple clang version 13.0.0 (clang-1300.0.29.30)

Thanks for your help in advance!

  • How are you compiling your code? My crystal ball says probably something like `g++ Assignment.cpp -o Assignment` which isn't compiling and linking `BST.cpp`. You need something like `g++ Assignment.cpp BST.cpp -o Assignment`. – Retired Ninja Feb 14 '22 at 00:12

0 Answers0