Jump to content
  • Advertisement
Sign in to follow this  
origil

Battle of the templates! :(

This topic is 4817 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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]

Share this post


Link to post
Share on other sites
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 begining
ListItem* listEnd; //A pointer to the list ending

ListItem* info; //The current item pointed at in the list

public:
CList();
bool New(T_classType* newItem); //Creates a new object right before the
//Many more functions I've cut out
//..
//..
}; //Class declaration end

template <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


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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-template
template <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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!