a compiler error with template function

J

Jess

Hi,

I have a template function that triggered some compiler error. The
abridged version of the class and function is:

#include<memory>

using namespace std;

template <class T>
class Vec{
public:
T* data;
T* avail;
T* limit;
std::allocator<T> alloc;

template<class In> void create (In,In);

};

template<class T,class In>
void Vec<T>::create(In i, In j){
data = alloc.allocate(j - i);
limit = avail = std::uninitialized_copy(i,j,data);
}

The compiler error was:
error: prototype for 'void Vec<T>::create(In, In)' does not match any
in class 'Vec<T>'
error: candidate is: template<class T> template<class In> void
Vec::create(In, In)
error: template definition of non-template 'void Vec<T>::create(In,
In)'

Another finding I have is that if I change the template function to:

template<class In, class T>
void Vec<T>::create(In i, In j){
data = alloc.allocate(j - i);
limit = avail = std::uninitialized_copy(i,j,data);
}

Then I get this even stranger error (because it says what I've
declared aren't available)

error: invalid use of undefined type 'class Vec<T>'
error: declaration of 'class Vec<T>'
error: template definition of non-template 'void Vec<T>::create(In,
In)'
In member function 'void Vec<T>::create(In, In)':
error: 'data' was not declared in this scope
error: 'alloc' was not declared in this scope
error: 'limit' was not declared in this scope
error: 'avail' was not declared in this scope

I guess I must have made some mistakes with the order of these type
parameters. Can someone please point out my mistakes?

Thanks,
Jess
 
O

Obnoxious User

Jess skrev:
Hi,

I have a template function that triggered some compiler error. The
abridged version of the class and function is:

#include<memory>

using namespace std;

template <class T>
class Vec{
public:
T* data;
T* avail;
T* limit;
std::allocator<T> alloc;

template<class In> void create (In,In);

};

template<class T,class In>
void Vec<T>::create(In i, In j){
data = alloc.allocate(j - i);
limit = avail = std::uninitialized_copy(i,j,data);
}

template<class T>
template<class In>
void Vec<T>::create(In i, In j){
data = alloc.allocate(j - i);
limit = avail = std::uninitialized_copy(i,j,data);
}
 
R

Rolf Magnus

Jess said:
Hi,

I have a template function that triggered some compiler error. The
abridged version of the class and function is:

#include<memory>

using namespace std;

template <class T>
class Vec{
public:
T* data;
T* avail;
T* limit;
std::allocator<T> alloc;

template<class In> void create (In,In);

};

template<class T,class In>
void Vec<T>::create(In i, In j){
data = alloc.allocate(j - i);
limit = avail = std::uninitialized_copy(i,j,data);
}

The compiler error was:
error: prototype for 'void Vec<T>::create(In, In)' does not match any
in class 'Vec<T>'
error: candidate is: template<class T> template<class In> void
Vec::create(In, In)
error: template definition of non-template 'void Vec<T>::create(In,
In)'

Well, that error message is quite self-explanatory, isn't it?
I guess I must have made some mistakes with the order of these type
parameters. Can someone please point out my mistakes?

You have a template template, so you have to define it like that:

template<class T>
template<class In>
void Vec<T>::create(In, In)
 
J

Jess

Well, that error message is quite self-explanatory, isn't it?


You have a template template, so you have to define it like that:

template<class T>
template<class In>
void Vec<T>::create(In, In)

What's the difference between template<class T, class In> void ....
and your example? Aren't they both template functions with two type
parameters?

Thanks!
Jess
 
R

Rolf Magnus

Jess said:
What's the difference between template<class T, class In> void ....
and your example? Aren't they both template functions with two type
parameters?

No. Yours is a non-templated function in a template class with two
parameters, mine is a template function with one parameter in a template
class with one parameter.
 
J

Jess

No. Yours is a non-templated function in a template class with two
parameters, mine is a template function with one parameter in a template
class with one parameter.

Just to make it clear, is the following reasoning correct?

a. you have:
template<class T>
template<class In>
void Vec<T>::create(In,In)

so the first "template<class T>" covers the class "Vec<T>" and the
second "template<class In>" covers (separately) the member function
"create", is this right?

b. I have:
template<class T, class In>
void Vec<T>::create(In,In)

so there's only one template, that covers the outer most Vec<T>, hence
"In" is dependent on the class Vec, hence create is not a template
function, is this also right?

Is it the case that the outermost "template" keyword covers the
outermost class/function parameter etc? If I have a nested class:

template<class A1,A2>
template<class B>
template<class C>
void X<A>::Y<B>::f(C);

Then X is a template class with parameters "A1" and "A2", Y is a
template class with parameter "B" and f is a template function with
parameter "C", these parameters are all independent to each other, is
this right?

I think if I have a template function with multiple parameters like:
template<class A, class B>
void func(A,B)

then it is always equivalent to:
template<class A>
template<class B>
void func(A,B).

Please correct me if I'm wrong. Thanks a lot!
Jess
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Just to make it clear, is the following reasoning correct?

a. you have:
template<class T>
template<class In>
void Vec<T>::create(In,In)

so the first "template<class T>" covers the class "Vec<T>" and the
second "template<class In>" covers (separately) the member function
"create", is this right?
Yes

b. I have:
template<class T, class In>
void Vec<T>::create(In,In)

so there's only one template, that covers the outer most Vec<T>, hence
"In" is dependent on the class Vec, hence create is not a template
function, is this also right?

Yes, so for this to work, it would have to be

template<class T, class Li>
void Vec<T, In>::create(In, In)

[skip some stuff that are over my head, but seems wrong to me]
I think if I have a template function with multiple parameters like:
template<class A, class B>
void func(A,B)

then it is always equivalent to:
template<class A>
template<class B>
void func(A,B).

No, the second one says that you have a function func which is
parameterized with A and then you have something else which is
parameterized by B, but there is nothing left to parameterize since
func is already parameterized by A.
 
J

Jess

template<class T, class Li>
void Vec<T, In>::create(In, In)

What is "Li" here? Is it an extra parameter?
No, the second one says that you have a function func which is
parameterized with A

Is this because of the outer "template said:
and then you have something else which is
parameterized by B,

but there is nothing left to parameterize since
func is already parameterized by A.

Yes, there's nothing else to parameterize, but is "func" not allowed
to parameterize with B as well, because there are two parameters A and
B in func? If func isn't allowed to take on another parameter (B
here), then what does the "B" in "func(A,B)" mean? Alternatively, if
I have:

template<class A>
void func(A,B)

and "B" is not mentioned anywhere else (i.e. func isn't a function of
a template class parameterized with B), then what does the declaration
above mean?

Thanks a lot!
Jess
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

What is "Li" here? Is it an extra parameter?

No, that's a typo, it should be 'In' (don't know how it became 'Li').
Is this because of the outer "template<class A>"?
Yes.


Is this because of the second "template<class B>"?
Yes.

Then, what is that "something else", since there's nothing left?

Exactly, and that's why it does not compile, VC gives me "error C3857:
'func': multiple template parameter lists are not allowed". Or in
English "You can't do that".
Yes, there's nothing else to parameterize, but is "func" not allowed
to parameterize with B as well, because there are two parameters A and
B in func?

It is, but that's not how you do it.
If func isn't allowed to take on another parameter (B here), then
what does the "B" in "func(A,B)" mean?

Nothing, it's just wrong, what you want to do is done like this:

Alternatively, if I have:

template<class A>
void func(A,B)

and "B" is not mentioned anywhere else (i.e. func isn't a function of
a template class parameterized with B), then what does the declaration
above mean?

Compiler error again, if B is not defined then it could be anything, but
the compiler needs to know what exactly it is.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,184
Messages
2,570,973
Members
47,530
Latest member
jameswilliam1

Latest Threads

Top