c++ class template instantiation

I'm trying to instantiate a class template pointer but unfortunately, vs2010 cannot compile it. The class I'm creating is a resource manager which contains a link list internally that stores data. The resource manager should be able to handle different data types so I declare it as a class template.
1
2
3
4
5
template <class T> class CResourceManager {
	// Data members
protected:
	CDLinkedList<T>* m_list;
	..


The internal link list is another class template which takes after the type of the resource manager. Both the linked list and manager are at a global scope.
The compiler gives an error on that last line.

1
2
3
4
..\resourcemanager.h(83): error C2143: syntax error : missing ';' before '<'
..\resourcemanager.h(104) : see reference to class template instantiation 'CResourceManager<T>' being compiled
..\resourcemanager.h(83): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
..\resourcemanager.h(83): error C2238: unexpected token(s) preceding ';'


I tried adding typename to the front as it seems like the compiler is parsing CDLinkedList<T>* as the variable name but that still doesn't fix the problem.
Any help would be appreciated.
Last edited on
missing header file?
for CDLinkedList<>

?
I did include the link list class before manager. vs instellisense seems to recognise the class too.
I'm not used to the cpp includes tho. Ill post the include order just incase:

1
2
3
4
5
6
7
8
9
10
11
12
13
// main.cpp

#include "DLinkedList.cpp"
#include "ResourceManager.cpp"
void main() { ....}

// DLinkedList.cpp
#include "DLinkedList.h"
..

// ResourceManager.cpp
#include "ResourceManager.h"
..


No includes in the link list and manager .h files
The standard says to use int main() instead of void main(), and not all compilers will agree to compile code with void main().

I hope you aren't trying implement your template class in your .cpp file. You can't really do that.

-Albatross

EDIT: Albatross's post count cannot count to 5.
Last edited on
You need one more argument to your template, it should be like this:

template <class T, template<class> class CDLinkedList> class CResourceManager {/*...*/}

OR

template <class T, template<class A> class CDLinkedList<A> > class CResourceManager {/*...*/}

Also, if you need to do something like this inside your template class:

CDLinkedList<T>::iterator it;

it won't work. You should do it like this:

typename CDLinkedList<T>::iterator it;

EDIT:

Albatross wrote:
I hope you aren't trying implement your template class in your .cpp file. You can't really do that.

Albatross is right. Since you work with templates, you must put everything (declarations and definitions) in a header file (.h/.hpp)
Last edited on
Since you work with templates, you must put everything (declarations and definitions) in a header file (.h/.hpp)

I somehow managed to implement my link list template in a seperate cpp file. Tested it in a smaller project and it works as expected so not sure if its breaking my manager.

1
2
3
4
5
// CDLinkedList.cpp
// Constructor
template <class T> CDLinkedList<T>::CDLinkedList() {
...
}


template <class T, template<class A> class CDLinkedList<A> > class CResourceManager {/*...*/
Is there a better workaround? the link list class only needs to follow the same data type that the manager accepts. Introducing another template var seems abit redundant.
This isn't a workaround! It's the right and only way to do it.

EDIT: Taken from the book Thinking in C++, Volume 1:

(Introduction to Templates)
(Template Syntax)
(Non-inline function definitions)
(Header files)
Even if you create non-inline function definitions, you’ll usually
want to put all declarations and definitions for a template into a
header file. This may seem to violate the normal header file rule of
“Don’t put in anything that allocates storage,” (which prevents
multiple definition errors at link time), but template definitions are
special. Anything preceded by template<...> means the compiler
won’t allocate storage for it at that point, but will instead wait until
it’s told to (by a template instantiation), and that somewhere in the
compiler and linker there’s a mechanism for removing multiple
definitions of an identical template. So you’ll almost always put the
entire template declaration and definition in the header file, for ease
of use.

There are times when you may need to place the template
definitions in a separate cpp file to satisfy special needs (for
example, forcing template instantiations to exist in only a single
Windows dll file). Most compilers have some mechanism to allow
this; you’ll have to investigate your particular compiler’s
documentation to use it.

Some people feel that putting all of the source code for your
implementation in a header file makes it possible for people to steal
and modify your code if they buy a library from you. This might be
an issue, but it probably depends on the way you look at the
problem: Are they buying a product or a service? If it’s a product,
then you have to do everything you can to protect it, and probably
you don’t want to give source code, just compiled code. But many
people see software as a service, and even more than that, a
subscription service. The customer wants your expertise, they want
you to continue maintaining this piece of reusable code so that they
don’t have to – so they can focus on getting their job done. I
personally think most customers will treat you as a valuable
resource and will not want to jeopardize their relationship with
you. As for the few who want to steal rather than buy or do original
work, they probably can’t keep up with you anyway.
Last edited on
Ah ok. It works when i put everything into a single file. Thanks for the help
Last edited on
Topic archived. No new replies allowed.