0

i get these error

C:\Users\SDRav\AppData\Local\Temp\ccCy0RiX.o:driver.cpp:(.text+0x1b): undefined reference to BST<char>::BST()' C:\Users\SDRav\AppData\Local\Temp\ccCy0RiX.o:driver.cpp:(.text+0x1eb): undefined reference toBST::insert(char const&)' C:\Users\SDRav\AppData\Local\Temp\ccCy0RiX.o:driver.cpp:(.text+0x228): undefined reference to BST<char>::traversePreorder(BSTNode<char>*)' C:\Users\SDRav\AppData\Local\Temp\ccCy0RiX.o:driver.cpp:(.text+0x263): undefined reference toBST::search(char const&) const' C:\Users\SDRav\AppData\Local\Temp\ccCy0RiX.o:driver.cpp:(.text+0x32b): undefined reference to BST<char>::deleteNode(char const&)' C:\Users\SDRav\AppData\Local\Temp\ccCy0RiX.o:driver.cpp:(.text+0x419): undefined reference toBST::leafCount(BSTNode*)' C:\Users\SDRav\AppData\Local\Temp\ccCy0RiX.o:driver.cpp:(.text+0x47c): undefined reference to BST<char>::search(char const&) const' C:\Users\SDRav\AppData\Local\Temp\ccCy0RiX.o:driver.cpp:(.text+0x4d5): undefined reference toBST::getSiblings(char const&)' collect2.exe: error: ld returned 1 exit status

but i have no idea why.... my guess is linking but i have check the headers and i still cant see it

my driver.cpp(main)

#include <iostream>

#include <iomanip>
#include "BST.h"
using namespace std;

int main()
{

    BST<char> op1;
    int action = 0;
    int numInsert = 0;
    char value;

cout<<"-------------------- MENU ----------------------"
        <<endl<<"1. Insert node(s)"
        <<endl<<"2. Traverse Preorder"
        <<endl<<"3. Search BST"
        <<endl<<"4. Delete node"
        <<endl<<"5. Leaf Count"
        <<endl<<"6. Sibling of a node"
        <<endl<<"7. Quit"
        <<endl<<"Please ent

 switch (action)
        {
            case 1:
                cout << "\n How many values would you liek to insert to a BST? ";
                cin>>numInsert;
                for(int i =0; i<numInsert;i++){
                    cout<<"What value would you like to insert to the list: ";
                    cin >> value;
                    op1.insert(value); //check
                }    
                break;
   ...

}

--- BST.h ---

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

using namespace std;

#ifndef BINARY
#define BINARY

template <typename DataType>

class BST
{
    public:
        BST();
        // ~BST();

        bool empty() const;
        /*------------------------------------------------------------------------
            Check if BST is empty.

            Precondition:  None.
            Postcondition: Returns true if BST is empty and false otherwise.
        -----------------------------------------------------------------------*/
        void insert(const DataType & item);
        void traversePreorder(BSTNode<DataType> * startPoint);
        bool search(const DataType & item)const;
        bool deleteNode(const DataType & item);
        int leafCount(BSTNode<DataType> * startPoint);
        DataType getSiblings(const DataType & item);




    private:
        BSTNode<DataType> * treeRoot;
        typedef BSTNode<DataType> * BSTNodePointer; 
        void search2(const DataType & item, bool & found,  BSTNodePointer & locptr, BSTNodePointer & parent) const;


};

please help

raven065
  • 13
  • 4
  • *but i have check the headers and i still cant see it* -- Let's see the header. – PaulMcKenzie May 10 '20 at 05:00
  • `BST::BST()` -- So where is this function actually located? That is what the linker is complaining about -- you called this function, but where is this function implemented? – PaulMcKenzie May 10 '20 at 05:06
  • I'm willing to bet that you have the implementation for the constructor in `BST.cpp`? [You can't do that](https://stackoverflow.com/q/495021/501250) (without an explicit template instantiation). Move the implementation into the header and delete the `BST.cpp` file. – cdhowie May 10 '20 at 05:09
  • g++ ".\*.cpp" -o .\Output.exe this is what i type in the terminal is vscode – raven065 May 10 '20 at 05:10
  • @cdhowie : your i do – raven065 May 10 '20 at 05:12
  • @raven065 Let us know if the errors go away after moving the implementation to the header file. When compiling the other units, the compiler needs to be able to see the implementation to instantiate the template. – cdhowie May 10 '20 at 05:15
  • @raven065 Your first mistake in finding the cause of the issue is that you looked in a header file. The header file you posted only includes declarations of member functions, not their actual implementations. So even if you were not using templates, your approach of trying to find the problem was wrong. If you had told us up front there was a BST.cpp file, then we would have known immediately the problem, since you are using a templated class. – PaulMcKenzie May 10 '20 at 05:16
  • i get a whole lot of other error... code fixes In file included from .\driver.cpp:4: .\BST.h: In instantiation of 'void BST::insert(const DataType&) [with DataType = char]': .\driver.cpp:38:37: required from here .\BST.h:77:34: error: lvalue required as left operand of assignment parent->getLeft()= locPtr; .\BST.h:80:36: error: lvalue required as left operand of assignment parent->getRight() = locPtr; – raven065 May 10 '20 at 05:17
  • Those are compiler errors now, not linker errors. Your attempt of moving the implementations in the header file failed, or you are doing something else wrong. Who wrote the original `BST.h`? Did you write it? If so, start again, but this time, put the implementation in those functions instead of creating a separate .`.cpp` file. – PaulMcKenzie May 10 '20 at 05:23

0 Answers0