-1

Have a simple error. Looked at a bunch of posts and checked the causes on my files, but couldn't seem to fix it. The functions being called from my BST.cpp file are throwing the error in the title ("identifier "buildTree" is undefined). The functions in question are 'buildTree' and 'performSearchBST'. The CPP file is included in main and the class templates are declared on all of the functions. Probably a simple error but was hoping someone could help point it out for me. Code listed below. As always thank you in advance for your assistance.

Main File:



//..
#include "BST.cpp"
#include "Node.h"
#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;

int main()
{
    //...
    ifstream filename("grocery_upc_database.csv");
    BST<UPC> tree = buildTree(filename); //build binary search tree of UPC objects

    string code;
    cout << "Please enter a UPC code(! to quit): ";
    cin >> code;
    while (code != "!")
    {

        long entry = stol(code); //convert user inputted string to type long int
        UPC key(entry);
        performSearchBST(tree, key);

        cout << "\nPlease enter a UPC code(! to quit): ";
        cin >> code;
    }

    return 0;
}

BST.cpp

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <fstream>
#include "BST.h"


using namespace std;

template <class T>
BST<T>::BST()
{
    root = NULL;
}
template <class T>
BST<T>::BST(long upcdata, string descrip)
{
    root->data = upcdata;
    root->descrip = descrip;
}
template <class T>
void BST<T>::buildTree(string inFile)
{
    string upcholder;
    string description;
    string line;
    ifstream file;
    file.open("grocery_upc_database.csv");
    while (getline(file, line))
    {
        stringstream ss(line);
        getline(ss, upcholder, ',');
        getline(ss, description, '\t');
        Node<T> newNode;
        newNode->data = stol(upcholder);
        newNode->descrip == description;
        insert(newNode);
    }
}

template <class T>
void BST<T>::insert(Node<T> *newNode)
{
    insert2(root, newNode);
}

template <class T>
void insert2(Node<T> *&root, Node<T> *newNode)
{
    if (root->data = NULL)
    {
        root = newNode;
    }
    else if (newNode->data < root->data)
    {
        insert2(root->left, newNode);
    }
    else
    {
        insert(root->right, newNode);
    }
}

template <class T>
long BST<T>::stol(string item)
{
    long i = atol(item.c_str());
    return i;
}

template <class T>
bool BST<T>::performSearchBST(BST<T> bst, UPC key)
{
    return performSearchBST2(bst,key);
}
template <class T>
bool performSearchBST2(BST<T> bst, UPC key)
{
    if (bst.root == NULL)
        return false;
    
    if (bst.root->data == key)
        return true;
    else if (root->data < key)
        return performSearchBST2(root->right, key);
    
    else
        return performSearchBST2(root->left, key);
    
    
}

BST.h

#include <iostream>
#include <string>
#include "Node.h"

using namespace std;

template <class T>
class BST
{
    Node<T> *root;

public:
    BST();
    BST(long key, string descrip);
    bool find(T item);
    bool performSearchBST(BST<T> bst, UPC key);
    void buildTree(string inFile);
    void insert(Node<T>* newNode);
    long stol(string item);
};
Duck
  • 13
  • 1
  • 4
  • `buildTree` is a member function of `BST` class. It needs to be called on some object of that class. You probably meant something like `BST tree; tree.buildTree(filename);` – Igor Tandetnik Nov 15 '20 at 05:19

1 Answers1

0

You have a simple error here.

First and foremost, your compiler doesn't know what is buildTree(String). You have a non-static member function whose name is BST<T>::buildTree(String).

Secondly, as said in the comments, you cannot use such a non-static member function without an object of that type, since it operates on a specific object.

You can get by either:

Using it as a member function, thus:

BST<UPC> tree{};
tree.buildTree(filename);

Or, transform the function into a static function that builds a tree, and it should look like:

static BST<T> buildTree(string inFile)
{
    \* Create and build the tree inside
       return the tree
    */
}

and then you can use it by:

BST<UPC> tree = BST<UPC>::buildTree(filename);

Which is the proper name of the function.

Also, when using templates, you need to declare and define the function within the same .hpp file see this question for more information.

Kerek
  • 1,106
  • 1
  • 8
  • 18
  • 1
    Thank you for your input! I should have mentioned in the main post, the issue is that I am not allowed to edit the text in main at all. I can add lines, but can't change the function call in particular. – Duck Nov 18 '20 at 00:45
  • The code itself seems faulty. The only solution I can think of is creating such a function with template **outside** of the class. – Kerek Nov 18 '20 at 10:42
  • @Duck I must say I cannot find a simple solution for that, I have added a remark you should read at the end of the answer, but couldn't find an easy way... – Kerek Nov 18 '20 at 11:00
  • 1
    I solved by moving the functions to the main (app) cpp file and that solved the issue. Thank you! – Duck Nov 19 '20 at 19:17