using templates with different typenames

Hi all,

I would like to use in one template class a different template class while both having different typenames. It results in the following compilation error:
ā€˜D’ was not declared in this scope

The declaration and definition of the template class is as follows:
1
2
3
4
5
6
7
8
9
10
11
12
#include "cc_where_class_2_is_def.cc"

template <typename T>
class class_1

{
	vector<T>vec;
	public:	
		class_1();
		class_1(class_2<D>* obj)
};
//.... 



How can I solve my problem.

thx
Last edited on
You can solve your problem by declaring 'D' in the appropriate scope, as your compiler kindly told you.. Writing in line 3: template <typename T, typename D>
should do the trick. BTW, you don't want to #include .cc files, and you don't want to write template code used in other compilation units in .cc files.
Thanks for your fast reply!

Yes that works, but I dont want to instantiate an object of class_1 like
 
class_1<T1,T2> object_of_class_1
.

To be more precise, I would like the user to use these two kind of classes as follows:
1
2
3
4
5
6
void main()
{
  class_2<int> obj_2;
  
  class_1<long> obj_1(&obj_2, ...)
}

So class_1 should only use obj_2 for retrieving some information by calling some of obj_2's member functions.

I think the compiler is able to resolve the <typename D = int> at compile time as class_2 is instantiated with <typename D = int> before the instantiation of class_1 with <typename T = long>. So somehow it should work, without mentioning <typename D> in the class_1 declaration.
Is there another way to solve this problem ?

thx again.
You can write a template constructor:
1
2
3
4
5
template <typename T> class C
{
...
  template <typename D> C(D* d) { }
};
Thank you very much for your reply, it solved the problem.

What I dont understand, what is it bad to include all the .cc files and how to solve this problem ?

thx again.
.cc - files are so-called "compilation units". They should be compiled one-by-one without linking etc.
Whenever you #include something, it is physically copied into the source which #includes it. If you do it with compilation units, you *will* get "multiple definition"-errors (i.e., the linker finds two definitions of the same function, since the code is compiled multiple times due to the #include-directives). Furthermore, you create unneccessary dependencies and make compilation time longer than needed.
Use .h-files for definitions and .cc (or .cxx or .cpp or whatever) for the compilation units, i.e. the actual source code (except when writing templates. They obviously have to be recompiled for every type, so the code has to be present in the cc-file, so it has to be #included, so they go in a .h-file)
Ookay thanks,

but one has a problem if one splits its code into a header and .c file when working with templates, at least when you work with a gcc compiler.

I have tried to solve it by predeclaration of the classes I wanted to use and it compiled. Is this a good solution ?

I get other errors when I want to use those template classes like this:
1
2
3
4
5
template <typename T> 
class_1<T>::class_1(class_2<T>* obj_2)
{
  obj_2->priv_func_of_class_2(this);
}


it says
that the priv_func_of_class_2 is private, and error within this context.

I dont know what the problem is ?
Last edited on

but one has a problem if one splits its code into a header and .c file when working with templates, at least when you work with a gcc compiler.

... as I stated, template code goes into .h-files

the priv_func_of_class_2 is private, and error within this context.


Yes, well, of course if it is private it can not be accessed from outside the class (if, of course, we assume that we are not in a friend function of class of this class or using tricks to override the private keyword, which we hopefully are not).
Topic archived. No new replies allowed.