smart pointer problem

M

Marko Pyhajarvi

good morning,

could anyone help me with this problem? i have made an abstract
container from which i have inherited a linkedlist class. this
linkedlist uses a smart pointer "counted_ptr<T>". there is a problem
now, that destructor definition doesn't go through compilation. below is
the code and comment from the compiler.

the problem seems to be that in destructor pointers next_ and object_
are not members of counted_ptr. what confuses me is that this same
doesn't appear in function "clear()" below!!?? i really don't get
understand anymore. do you know where i have made wrong? below is also
the struct which is located inside the linkedlist class.

Error E2315 mlinkedlist.h 69: 'object' is not a member of
'counted_ptr<MLinkedList<int>::ListNode>', because the type is not yet
defined in function MLinkedList
<int>::~MLinkedList()
Error E2315 mlinkedlist.h 70: 'object' is not a member of
'counted_ptr<MLinkedList<int>::ListNode>', because the type is not yet
defined in function MLinkedList
<int>::~MLinkedList()
Error E2315 mlinkedlist.h 72: 'next' is not a member of
'counted_ptr<MLinkedList<int>::ListNode>', because the type is not yet
defined in function MLinkedList<i
nt>::~MLinkedList()

----------------------------------------------------
template <class T>
MLinkedList<T>::~MLinkedList()
{
counted_ptr<ListNode> *removal;
current_ = first_;
while (current_ != 0)
{
if (current_->object != 0)
delete current_->object;
removal = current_;
current_ = current_->next;
delete removal;
removal = 0;
}
}

template <class T>
void MLinkedList<T>::clear()
{
counted_ptr<ListNode> *removal;
current_ = first_;
while (current_ != 0)
{
removal = current_;
current_ = current_->next;
delete removal;
removal = 0;
}
}

struct ListNode
{
T *object;
ListNode *next;
ListNode *previous;
};
counted_ptr<MLinkedList<T>::ListNode> *first_;
counted_ptr<MLinkedList<T>::ListNode> *current_;
int size_;
 
P

Peter Koch Larsen

Marko Pyhajarvi said:
good morning,

could anyone help me with this problem? i have made an abstract
container from which i have inherited a linkedlist class. this
linkedlist uses a smart pointer "counted_ptr<T>". there is a problem
now, that destructor definition doesn't go through compilation. below is
the code and comment from the compiler.

the problem seems to be that in destructor pointers next_ and object_
are not members of counted_ptr. what confuses me is that this same
doesn't appear in function "clear()" below!!?? i really don't get
understand anymore. do you know where i have made wrong? below is also
the struct which is located inside the linkedlist class.

Error E2315 mlinkedlist.h 69: 'object' is not a member of
'counted_ptr<MLinkedList<int>::ListNode>', because the type is not yet
defined in function MLinkedList
<int>::~MLinkedList()
Error E2315 mlinkedlist.h 70: 'object' is not a member of
'counted_ptr<MLinkedList<int>::ListNode>', because the type is not yet
defined in function MLinkedList
<int>::~MLinkedList()
Error E2315 mlinkedlist.h 72: 'next' is not a member of
'counted_ptr<MLinkedList<int>::ListNode>', because the type is not yet
defined in function MLinkedList<i
nt>::~MLinkedList()
[snip]

Error E2315 indicates that you have a forward declaration of the class
object:

class object;

In this case there is not much you can do with a variable of that type.
Specifically you can not delete an object of this type:
object *o; // legal
delete o; // illegal - as is (btw) o = new object;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top