0

I'm pretty new to binary search trees. I've created a template class for a binary search tree so that it can handle class objects. Now I'm trying to test it in main, and I'm reading in dates from a text file into a 'Date' object of class Date, using a while loop until the end of the file. After the first iteration, I can see that my first date has successfully been entered into the tree when I check through the CodeBlocks debugger. Now, when going through the second iteration, the program reads and sets the dates successfully to the Date Object, outputs it with my cout statements, and then pauses for a bit and crashes.

At the instance where it pauses or crashes, in my debugger, I can see that it is endlessly calling my function overload 'bool operator==(const Date& firstDate, const Date& secondDate)' which I use to compare whether the dates are equal and no matter how many times I try to step into the next line it wont let me, it is just stuck on this line. So I imagine something is wrong with my operator overload?

Function overload where it gets stuck:

bool operator==(const Date& firstDate, const Date& secondDate)
{
    if(firstDate==secondDate) // The debugger is pointing to this line
    {
        return true;
    }
    return false;
}

Main:

ifstream infile("date.txt");

        string date1;

        Bst<Date> dateTree;

        while(getline(infile, date1))
        {
            Date dateObj;
            stringstream date(date1);
            string day;
            string month;
            string year;

            getline(date,day,'/');
            getline(date,month,'/');
            getline(date,year, ' ');
            cout << day << endl;
            cout << month << endl;
            cout << year << endl;

            dateObj.SetDay(day);
            dateObj.SetMonth(month);
            dateObj.SetYear(year);

            dateTree.insertNode(dateObj);
        }

        dateTree.inOrderTraversal();

        return 0;
}

This is my node insertion code:

template <class T>
void Bst<T>::insertNode(const T& insertItem)
{
    nodeType<T> *current;
    nodeType<T> *trailCurrent = nullptr;
    nodeType<T> *newNode;

    newNode = new nodeType<T>;
    newNode->info = insertItem;
    newNode->lLink = nullptr;
    newNode->rLink = nullptr;

    if(root == nullptr)
        root = newNode;
    else
    {
        current = root;
        while(current != nullptr)
        {
            trailCurrent = current;
            if(current->info == insertItem)
            {
                cout << "The item to be inserted is already ";
                cout << "in the tree -- duplicates are not "
                << "allowed." << endl;
                return;
            }
            else if(current->info > insertItem)
                current = current->lLink;
            else
                current = current->rLink;
        }
        if(trailCurrent->info > insertItem)
            trailCurrent->lLink = newNode;
        else
            trailCurrent->rLink = newNode;

    }
}
2
  • @molbdnilo I've added my node insertion code, if you can help me identify what may be wrong Commented May 20, 2020 at 8:14
  • 1
    bool operator==(const Date& firstDate, const Date& secondDate) { if (firstDate==secondDate) — This is endless recursion. You likely need to compare member variables of Date (possibly via their getters) to check whether 2 dates are equal. Commented May 20, 2020 at 8:17

1 Answer 1

2

The test firstDate==secondDate compiles to:

operator==(firstDate, secondDate)

which calls your bool operator==(const Date& firstDate, const Date& secondDate) ad infinitum. You need to compare the components of the Date object. I don't know how it is defined, but maybe something like the following:

bool operator==(const Date& firstDate, const Date& secondDate) {
    return std::tie(firstDate.year, firstDate.month, firstDate.day) == 
           std::tie(secondDate.year, secondDate.month, secondDate.day);
}
Sign up to request clarification or add additional context in comments.

1 Comment

I just did that using my getters, and now there seems to be no issue. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.