I can't find the mistake

Hi!

I made a list container which as close as possible to the STL one.
The remove method was implemented in this way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
template<class T>
void Container<T>::Remove(const T& t) {
    if(first)
    {
        Node*prev = nullptr;
        Node*temp = first;

        while(temp && t != temp->info) // t != temp->info makes my program stop working
        {
            prev = temp;
            temp = temp->next;
        }

        if(temp)
        {
            if(!prev)
                first = temp->next;
            else
                prev->next = temp->next;
            delete temp;
        }

    }
}

/*
The instruction highlighted makes my program to stop working.
I think the function is well done though, so I checked the
comparison(and his opposite) operator in the class
I am passing as T parameter, which is a smart pointer class to a hierarchy:
*/

template<class T>
bool DeepPtr<T>::operator== (const DeepPtr<T>& dp) const {
    return *this == dp; //tipo diverso
}

template<class T>
bool DeepPtr<T>::operator!= (const DeepPtr<T>& dp) const {
    return *this != dp;
}


Honestly I can't see where the mistake is, could please someone tell me if there's something wrong?
Last edited on
Have you run the program with your debugger?

What is the value of t and temp->info at the time of the crash?

What is the type of t? What is the type of temp->info?

What exactly is the error message when your program crashes?

Lines 35 and 40 are recursive.
Thank you very much for answering! @jlb and @AbstractionAnon

Have you run the program with your debugger?

I am using Qt Creator and viewing the reoprt on the Application Output tab.


What is the value of t and temp->info at the time of the crash?

I can't know it since there's no overloaded output operator in the DeepPtr class.

What is the type of t? What is the type of temp->info?

Both of them are of the same type, I checked it out doing:
if(typeid(t) == typeid(temp->info))
cout<<"types are matching"<<endl;

What exactly is the error message when your program crashes?

17:11:30: The program has unexpectedly finished.


Lines 35 and 40 are recursive.

You are right! This actually fixed it!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template<class T>
bool DeepPtr<T>::operator== (const DeepPtr<T>& dp) const {
    if(p == dp.p)
        return true;
    else
        return false;
}

template<class T>
bool DeepPtr<T>::operator!= (const DeepPtr<T>& dp) const {
    if(p != dp.p)
        return true;
    else
        return false;
}

Last edited on
I can't know it since there's no overloaded output operator in the DeepPtr class.

This is where the debugger comes in. You should be able to view the variables at the time of the crash if you run the program with your debugger.

They're still recursive. Ine line 3, the equality operator calls itself. In line 11, the inequality operator calls itself.

The point of overriding the equality operatopr is to define some logic by which you can tell whether the classes are equal.


If you'd taken jlb's advice to use a debugger, you would have seen this for yourself.

EDIT: Apologies, I misread your code.

I still strongly recommend you take jlb's advice.
Last edited on
MikeyBoy wrote:
They're still recursive.

Not necessarily. The OP hasn't shown the type of member p. Assuming p is a simple pointer, then a comparison of two simple pointers is not recursive.
Oops, yes, I misread the code. Apologies!
For completeness:

This is where the debugger comes in. You should be able to view the variables at the time of the crash if you run the program with your debugger.


I checked it out before (recursive use of DeepPtr operator==) and after (fixed code) with debugger: (I even took screenshots but it looks like they can't be uploaded :( )

Anyway it's a really useful tool! Thank you @jlb and @Mikey Boy.

Not necessarily. The OP hasn't shown the type of member p. Assuming p is a simple pointer, then a comparison of two simple pointers is not recursive.


@AbstractionAnon
Thank you very much for the detailed explaination, it was really appreciated!

Oops, yes, I misread the code. Apologies!

No worries, thank you for being here to help and for making me think twice about it!
Topic archived. No new replies allowed.