Battle of the templates! :(

Started by
4 comments, last by _winterdyne_ 18 years, 7 months ago
I'll try to explain this before I get run over by more compiler errors [smile]. I've got a template for a class where the class is both defined and declared in the header file. Whenever I compile the project, which consists of: main.cpp - which just uses the template class templateClass.h - which contains the template class I get plenty of these errors:

"main.cpp line 16:instantiated from here"
"156 templateClass.h no match for `CMike::SInts & != int'"

CMike is an ordinary class containing the SInts struct definition, used as the typename for the template class found in templateClass.h. I've been battling these errors for hours and I must say, this template thingy is quite annoying. If anyone has any tips for me or if I can be pointed to a basic template tutorial I'd be grateful. Thanks in advance, Ori
The Department of Next Life - Get your Next-Life Insurance here!
Advertisement
Give us a little more code, please.

"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]

Templates are dead useful - commonly used to make smart pointers if you tend to forget what made something.

template <class T> class SmartPtr;{private:     T* m_pPointee;public:     SmartPtr(T* ptr) { m_pPointee = ptr; m_pPointee->incRef(); };     ~SmartPtr() { m_pPointee->decRef(); };         inline T* operator->() { return m_pPointee; };     // For a real smart pointer, you also need assignment, etc.     // Don't forget a template casting operator or you won't get     // inheritance working properly!     // This is cut down :-)}


Okay, what this is saying is:
I'm a template where I'll take any class T, called SmartPtr.

Within the template and template code, you can use T as an arbitrary typename.
Infact you can also use template <typename T>.

This way, you can write a small amount of code, and let the compiler make the rest for you - as long as T implements any method you call on it inside the template you can do

You declare it like this, when you want it:
 SmartPtr<SomeRefCountObject> sptr(pRefCountObject);


SomeRefCountObject MUST implement incRef() and decRef() since the template uses them.

A couple of really REALLY good books on this are Effective C++ and More Effective C++ by Scott Meyers. Everyone should have these.

Hope this helps.

[Edited by - _winterdyne_ on September 6, 2005 10:47:05 AM]
Winterdyne Solutions Ltd is recruiting - this thread for details!
Thanks. I think I followed the guidelines of the code you posted. Still can't find the problem.
Anyhow, I tried to avoid simply posting the code because there's really enough of it and I don't want to waste everyone's time but I guess my first post wasn't very clear...

Basically there are 2 files.
One is the header file containing the CList class which contains a list of many ListItem structures with each of them containing a changing type.

PrList.h:
[SOURCE]template <typename T_classType>class CList{private: struct ListItem		//A single list memeber { T_classType item;       //List single data object ListItem* next; };ListItem* listStart;	//A pointer to the list beginingListItem* listEnd;	    //A pointer to the list endingListItem* info;		//The current item pointed at in the listpublic:CList();bool New(T_classType* newItem);	//Creates a new object right before the//Many more functions I've cut out//..//..}; //Class declaration endtemplate <typename T_classType>bool CList<T_classType>::New(T_classType* newItem){//Do something}[/SOURCE]


The second file is main.cpp where the class is being used.

[SOURCE]#include"PrList.h"void main(){CList<int*> MyList;  //I've tried many other forms and still errors}//End.Generated errors:1) 174 prlist.h   instantiated from `CList<int *>::~CList()'2)65 main.cpp     instantiated from here[/SOURCE]


Any ideas?
Thanks in advance,
Ori


The Department of Next Life - Get your Next-Life Insurance here!
This is a complete guess because you haven't provided any relevant details. Somewhere in your code you have something like this:
    template< typename T >    foo( T & x )    {        int y;        ...        if ( x != y )        ...    }    ...    CMike::SInts z;    foo( z );    
First of all, user classes are not provided with default == and != operators. If you create class and you do not provide operators for == and !=, you can't use those operators. Next, because you are comparing your class to an int, you need to either provide a conversion operator ("operator int()") or a overload the != operator so that it can compare your class with an int ("operator !=( int x )"). However, I'll bet that you don't really want to do this and something else is wrong.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Your error is probably because you're using a non-templated struct from within the template to contain the template pointer, which is of an undefined type within the template.

Try templating ListItem as well:

[source lang="cpp]template <class T> class CList;{public:// This is a template-in-a-templatetemplate <class TItem> struct ListItem{ TItem* pListItem; ListItem * pNext};protected:   // T is a valid typename because this is a template<T>   ListItem<T>* pStart;   ListItem<T>* pEnd;   ListItem<T>* pCurrent; ...};



Try this, should work.
Winterdyne Solutions Ltd is recruiting - this thread for details!

This topic is closed to new replies.

Advertisement