Page 1 of 2

Learning C++

Posted: Thu Jun 28, 2012 3:51 am
by Phantom
People!! I can say I am re-starting in C++. My friend Robbin has been orientating me. But... I can not molest him more... The problem is the next. I has been doing some exercises were I have to use a queue. Now... I has been receiving Runtime Error in these exercises. I supouse the problem is in my queue. Pleace, if you can do any appoint about any suggestion or error, please, do it!! My queue with the Robbin´s help is the next:

Code: Select all

template <class T>
class Node
{
public:
    T Value;
    Node(T);
    Node<T>* Next;
};

template <class T>
Node<T>::Node(T val)
{
    Value = val;
    Next = NULL;
}

template <class T>
class Queue
{
    Node<T>* act;
    Node<T>* last;
public:
    Queue();
    ~Queue();
    void Enqueue(T);
    bool Dequeue(T&);
};

template <class T>
Queue<T>::Queue()
{
    act = NULL;
    last = NULL;
}

template <class T>
Queue<T>::~Queue()
{
    Node<T> * nodito=act;
    while(act!=NULL)
    {
        act=act->Next;
        nodito->Next=NULL;
        delete nodito;
	nodito=act;
    }
    delete act;
    act=NULL;
    last=NULL;
}

template <class T>
void Queue<T>::Enqueue(T val)
{
    if(act!=NULL)
    {
        last->Next = new Node<T>(val);
        last = last->Next;
    }
    else
    {
        last = new Node<T>(val);
        act = last;
    }
}

template <class T>
bool Queue<T>::Dequeue(T &result)
{
    if(act == NULL)return false;
    result = act->Value;
    Node<T>* temp = act;
    act = act->Next;
    temp = NULL;
    delete temp;
    if(act == NULL)delete last;
    return true;
}

Re: Learning C++

Posted: Thu Jun 28, 2012 8:14 pm
by Robbin
Hello again!!
I told you not to worry for anything..
just tell me if you need something..
Now I finally finished seeing your code.. and I think there's another litle problem on your code.. in the dequeue function..
This is your function:
template <class T>
bool Queue<T>::Dequeue(T &result)
{
if(act == NULL)return false;
result = act->Value;
Node<T>* temp = act;
act = act->Next;
temp = NULL;
delete temp;
if(act == NULL)delete last;
return true;
}

your'e leaving trash here..
before you say temp=NULL; you have to write temp->Next=NULL;
then you can erase the step when you say temp=NULL;
if you just write temp->Next=NULL; delete temp; that's right..
and may be you can quit the senctence delete last.. and put last=NULL;

Re: Learning C++

Posted: Thu Jun 28, 2012 8:19 pm
by Robbin
Question, phantom:
how do you invoke the functions??

Re: Learning C++

Posted: Fri Jun 29, 2012 1:09 pm
by Phantom
Wait a moment; my problem is that I don’t see yet the difference between:
temp = NULL; and delete temp;
Can you explain me that very well?

I invoke the functions normally, as I do it in all the languages:
jose = pepe.Friend();

Re: Learning C++

Posted: Fri Jun 29, 2012 1:28 pm
by Robbin
The problem is that temp has a field named Next, and another fields, but, Next is an apointer..
when you say delete temp you restore all the occuped memory for temp, otherwise you just say that temp is a null field, but the old value of temp still in memory.. you don´t erase it..
please send me back your e-mail and I will send you an image for explain you that..

PD: I can send you the conference about aponters in c++, and linked lists and so.. if you want it..

Re: Learning C++

Posted: Fri Jun 29, 2012 1:29 pm
by Robbin
The problem is that temp has a field named Next, and another fields, but, Next is an apointer..
when you say delete temp you restore all the occuped memory for temp, otherwise you just say that temp is a null field, but the old value of temp still in memory.. you don´t erase it..
please send me back your e-mail and I will send you an image for explain you that..

PD: I can send you the conference about aponters in c++, and linked lists and so.. if you want it..

Re: Learning C++

Posted: Tue Jul 03, 2012 5:22 pm
by Phantom
Yes!! I want it!! But… now we have problems with the e-mail in my faculty. Send me that to my internet e-mail. I´ll try to check it. My internet e-mail is in my profile, but I´ll give you now… Write me to: alejandrobez@gmail.com
I’ll be grateful with you.

Re: Learning C++

Posted: Thu Jul 05, 2012 3:32 pm
by Robbin
sorry, phantom, but I can't arrive to an a-mail if it's out of Cuba, or if it's international, I have no rights

Re: Learning C++

Posted: Thu Jul 05, 2012 3:34 pm
by Robbin
If you could enter on my faculty, may be you can obtain those things..
try ftp://10.30.150.4

Re: Learning C++

Posted: Wed Jul 11, 2012 1:00 pm
by Phantom
:( I couldn't enter... Is a shame that you can't arrive to an a-mail if it's out of Cuba... I have not way to exchange programs and informaton with you... :?