🔒 Locked
C++ Template problem
Advertisement
Original Post
there's probably a quick fix for this. My compiler (VS.net) is generating the faollowing error with my templated linked list class:
error C2989: 'List' : template class has already been defined as a non-template class
error C2989: 'ListItr' : template class has already been defined as a non-template class
I don't understand why it's puking that up. I specified that they were templated classes by writing "template -class Object- above and using "Object" for variable definitions. Those - are greater than and less than signs, html junk,
The code is divided into List.h w/ list.cpp, listItr.h, and listnode.h. the last two only have one-liner function source, so i'm not making a .cpp for them. Any ideas?
[edited by - chipmunk1886 on March 11, 2003 12:42:34 PM]
i thought someone might be able to see the problem based on the description i gave above. here ya go:
List Class Declaration:
The ListNode class:
The List Iterator Class
hope that helps.
List Class Declaration:
#include "listItr.h"
#include "listnode.h"
template <class Object>
class List
{
public:
List();
List(const List& rhs);
~List();
bool IsEmpty() const;
void makeEmpty();
ListItr<Object> zeroth() const;
ListItr<Object> first() const;
void insert(const Object& x, const ListItr<Object>& p);
ListItr<Object> find(const Object& x)const;
ListItr<Object> findPrevious(const Object& x) const;
void remove(const Object& x);
void Print()const;
const List & operator =(const List & rhs);
private:
ListNode<Object> *header;
The ListNode class:
template <class Object>
class ListNode
{
public:
ListNode( const Object& theElement = Object(), ListNode* n = 0)
:element (theElement), next(n) {}
Object element;
ListNode* next;
friend class List;//<Object>;
friend class ListItr;//<Object>;
};
The List Iterator Class
template <class Object>
class ListItr
{
public:
ListItr() :current(0) {}
bool IsPastEnd() const
{
return current == 0;
}
void advance()
{
if ( !IsPastEnd() )
current = current->next;
}
const Object& retrieve() const
{
if ( IsPastEnd() )
{
//throw BadIterator();
}
return current->element;
}
private:
ListNode<Object> *current;
ListItr(ListNode<Object> *theNode) : current(theNode){};
friend class List<Object>; //grant access to ListItr constructor
};
hope that helps.
Minimalistic example (works on g++)
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
template<class Obj> class Bar;
template<class Obj> class Foo
{
friend class Bar<Obj>;
};
template<class Obj> class Bar
{
};
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
You have to forward declare the List class as a template before declaring it as a friend class. So in listnode.h and listItr.h, put
EDIT: Put a tested solution instead.
EDIT2: Fixed formatting.
[edited by - cgoat on March 11, 2003 4:54:28 PM]
template<class Obj> class List;before the declaration of each class. Then you just use:
friend class List;inside those classes.
EDIT: Put a tested solution instead.
EDIT2: Fixed formatting.
[edited by - cgoat on March 11, 2003 4:54:28 PM]
thanks for the help everyone. but, unfortunately my compiler is still generating errors. I''ve tried many variations of your suggestions and no dice.
i''m not so much interested in templating as messing around with data structures, so I can just use a typedef. I was hoping that they could be templated though, darn.
i''m not so much interested in templating as messing around with data structures, so I can just use a typedef. I was hoping that they could be templated though, darn.
Sure you can, you just have to use magic, er I mean "export".
No, tbe only compiler I know of that supports export is the EDG C/C++ front end. That''s only a technicality though, because as far as I know, that front end version has yet to be implemented in any of it''s reseller''s compilers. I think Comeau''s compiler is on timeline to get compliance in about three months.
SO MSVC++ 6.0 using export will not work? I have "The Complete Reference C++" and it talks about export keyword being added to C++ along with typename. I tried it in MSVC++ 6.0 and it wants a storage class? if I remember right? Could someone post code on the correct way to use it would be even if MSVC++ doesn''t work with it. Thanks
Comeau is compliant - it supports export - look, about half way down, at ''What''s new?''
It has done for a few months.
It has done for a few months.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement