Skip to main content
gamedev.net
🔒 Locked

C++ Template problem

Started by chipmunk1886 Mar 11, 2003 at 11:38 AM 10 replies 3.2k views
Original Post
chipmunk1886
chipmunk1886
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]
-Ose
CpMan
CpMan
Can you show us some code?



Gamedev for learning.
libGDN for putting it all together.
An opensource, cross platform, cross API game development library.
VSEDebug Visual Studio.NET Add-In. Enhances debugging in ways never thought possible.
chipmunk1886
chipmunk1886
i thought someone might be able to see the problem based on the description i gave above. here ya go:

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.


-Ose
Fruny
Fruny
Minimalistic example (works on g++)


  
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 ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
cgoat
cgoat
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
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]
chipmunk1886
chipmunk1886
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.
-Ose
MadKeithV
MadKeithV
If list is a template class, you can _NOT_ separate it into a .h and a .cpp file. All of the implementation needs to go in the .h.
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
Shannon Barber
Shannon Barber
Sure you can, you just have to use magic, er I mean "export".
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
DerekSaw
DerekSaw
Do VS.Net really can understand export?
"after many years of singularity, i'm still searching n the event horizon" o
SiCrane
SiCrane
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.
MARS_999
MARS_999
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
Qw3r7yU10p!
Qw3r7yU10p!
Comeau is compliant - it supports export - look, about half way down, at ''What''s new?''

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.