For some reason I keep getting the errors:
"cop4530::BST<int>::buildFromInputString(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)", referenced from:

"cop4530::BST<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::buildFromInputString(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)", referenced from:

Im positive that it has to do with something with the function that that begins at line 114, please help.

Recommended Answers

All 7 Replies

//Header File
#ifndef BST_H 
#define BST_H
#include <iostream>
#include <string>
#include <queue>
#include <sstream>
using namespace std;
const int default_threshold_value = 1;
namespace cop4530
{

    template <typename T>    
    class BST
    {
    public:

        BST(int th);//Constructor
        BST(const string, int th);
        BST(const BST&);
        ~BST();
        const T & findMin() const;
        const T & findMax() const;

        void buildFromInputString(const string input);
        const BST & operator= (const BST &);
        bool empty();
        void printInOrder() const;
        void printLevelOrder() const;
        int numOfNodes() const;
        int height() const;
        void makeEmpty();
        void insert(const T& v);
        void remove(const T& v);
        bool contains(const T& v);
        int getThreshholdValue();

    private:
        struct BSTNode
        {
            T value;
            BSTNode(T v, BSTNode *t,BSTNode *x):value(v),leftchild(t),rightchild(x){}
            BSTNode *leftchild;
            BSTNode *rightchild;
        };

        BSTNode *root;
        int thresholdValue;
        void printInOrder(BSTNode *t) const;
        void printLevelOrder(BSTNode *t) const;
        BSTNode * findMin (BSTNode *t) const;
        void makeEmpty(BSTNode* &t);
        void insert(const T& v, BSTNode *&t);
        void remove(const T& v, BSTNode *&t);
        bool contains(const T& v, BSTNode *&t);
        int numOfNodes(BSTNode *t) const;
        int height(BSTNode *t) const;
        BSTNode * clone(BSTNode *t) const;
    };
#include "bst.hpp"
}
#endif 
***********************************
//Implementation File
#include "bst.h"
#include <iostream>
#include <string>
#include <vector>
#include <sstream>


using namespace std;
using namespace cop4530;
const int default_threshold_value = 1;
template <typename T>
BST <T>::BST(int th=default_threshold_value)
{
    thresholdValue = th;
    root = new BSTNode(NULL, NULL, NULL);
}

template <typename T>
BST <T>::BST(const string input,int th=default_threshold_value)
{
    thresholdValue = th;
    buildFromInputString(input);
}

template<typename T>
const BST<T> & BST<T>::operator=(const BST & rhs)
{
    if (this !=&rhs)
    {
        makeEmpty();
        root = clone(rhs.root);

    }
    return *this;
}

template <typename T>
BST <T>::BST(const BST& rhs)
{
    *this = rhs;
}

template <typename T>
BST <T>::~BST()
{
    makeEmpty();
}

//********Help here
template<typename T>
void buildFromInputString(const string input)
{
    istringstream iss(input);
    while(iss)
    {
        T temp_token;
        iss >> temp_token;
        insert(temp_token);
    }
}

template<typename T>
void BST <T>::printInOrder()const
{
    cout << endl;
    printInOrder(root);
}

template<typename T>
void BST <T>::printLevelOrder()const
{
    cout << endl;
    printLevelOrder(root);
}

template<typename T>
void BST <T>::insert(const T& v)
{
    insert(v,root);
}

template<typename T>
void BST <T>::remove(const T& v)
{
    remove(v, root);
}

template<typename T>
void BST <T>::makeEmpty()
{
    makeEmpty(root);
}

template<typename T>
void BST <T>::makeEmpty(BSTNode* &t)
{
    if (t != NULL)
    {
        makeEmpty(t->leftchild);
        makeEmpty(t->rightchild);
        delete t;
    }
    t = NULL;
}

template<typename T>
int BST <T>::height() const
{
    return(height(root));
}

template<typename T>
int BST <T>::numOfNodes() const
{
    return(numOfNodes(root));
}


template<typename T>
bool BST <T>::contains(const T& v)
{
    return contains(v,root);
}

template<typename T>
bool BST <T>::empty()
{
    return true;
}

/** Start Private Functions **/
template<typename T>
void BST <T>::printInOrder(BSTNode *t) const
{
    if(t != NULL)
    {
        printInOrder(t->leftchild);
        cout << t->value;
        printInOrder(t->rightchild);
    }
    return;

}

template<typename T>
void BST <T>::printLevelOrder(BSTNode *t) const
{
    queue<BSTNode*>printQueue;
    printQueue.push(t);
    while(!printQueue.empty())
    {
        if(t->leftchild != NULL)
            printQueue.push(t->leftchild);
        if(t->rightchild != NULL)
            printQueue.push(t->rightchild);
        cout << t->value;
        t = printQueue.front();
        printQueue.pop();
    }

    return;
}

template<typename T>
void BST <T>::insert(const T& v, BSTNode *&t)
{
    if(contains(v,t) && t != NULL)
    {
        if (v < t->value)
        {
            insert(v, t->leftchild);
        }
        else if (v > t->value)
        {
            insert(v, t->rightchild);
        }
        else;
    }
} 

template<typename T>
void BST <T>::remove(const T& v, BSTNode *&t)
{
    if (t == NULL)
    {
        return;
    }
    if (v < t->value) 
    {
        remove(v, t->leftchild);
    }
    else if (t->value < v)
    {
        remove(v,t->rightchild);
    }
    else if (t->leftchild != NULL && t->rightchild != NULL)
    {
        t->value = findMin(t->rightchild)->value;
        remove(t->value,t->rightchild);
    }
    else 
    {
        BSTNode *oldNode = t;
        t = (t->leftchild != NULL) ? t->leftchild : t->rightchild;
        delete oldNode;
    }

}

template<typename T>
bool BST <T>::contains(const T& v, BSTNode *&t)
{
    if (t == NULL)
    {
        return false;
    }
    else if (v < t->value)
    {
        return contains(v, t->leftchild);
    }
    else if (t->value < v)
    {
        return contains(v, t->rightchild);
    }
    else {
        return true;
    }
}

template<typename T>
typename BST <T>::BSTNode * BST<T>::findMin(BSTNode *t)const
{
    if (t== NULL)
    {
        return NULL;
    }
    if (t->leftchild == NULL)
        return t;

    return findMin(t->leftchild);
}



template<typename T>
typename BST <T>::BSTNode * BST<T>::clone(BSTNode *t) const
{
    if (t == NULL)
    {
        return NULL;
    }
    return new BSTNode(t->value, clone(t->leftchild), clone(t->rightchild));

}

template<typename T>
int BST <T>::numOfNodes(BSTNode *t) const
{
    if(t == NULL)
        return(0);

    return(numOfNodes(t->rightchild) + numOfNodes(t->leftchild) + 1);
}

template<typename T>
int BST <T>::height(BSTNode *t) const
{
    if(root == NULL)
        return(-1);
    else if(t == NULL);
    return(0);
    return(max(height(t->leftchild), height(t->rightchild)) + 1);
}

template<typename T>
int BST<T>::getThreshholdValue() 
{
    return thresholdValue;
}

The "errors" you posted are part of the stack trace, not the actual error message. Can you post the entire thing?

This is all it says:

"cop4530::BST<int>::buildFromInputString(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)", referenced from:

  _main in main.o


  cop4530::BST<int>::BST(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)in main.o

"cop4530::BST<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::buildFromInputString(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)", referenced from:

  cop4530::BST<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::BST(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)in main.o

ld: symbol(s) not found

collect2: ld returned 1 exit status

ld: symbol(s) not found

That means the linker doesn't see a definition for the stated function or method. After actually looking at your code, you can't split up a template class into multiple files like that. The method definitions must be in the header.

So basically in order for my code to work everything I have do everything within the header file?

So basically in order for my code to work everything I have do everything within the header file?

Yup.

As your class declaration says, buildFromInputString is part of its structure. But when you defined the functions of your class, particularly buildFromInputString function, I think you simply missed to type something. Look at what you've typed in line 114:

template<typename T>
void buildFromInputString(const string input)
{
    istringstream iss(input);
    while(iss)
    {
        T temp_token;
        iss >> temp_token;
        insert(temp_token);
    }
}

...ain't that supposed to be like this:

template<typename T>
void BST<T>::buildFromInputString(const string input)
{
    istringstream iss(input);
    while(iss)
    {
        T temp_token;
        iss >> temp_token;
        insert(temp_token);
    }
}

You missed the 'BST<T>::'. Thus when this function started from being referenced somewhere else in your program, you will get this linker error since it would fail to find the definition of the said function.

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.