473,383 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

a compiler error with template function

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<Talloc;

template<class Invoid 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 Ttemplate<class Invoid
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

May 6 '07 #1
8 5289
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<Talloc;

template<class Invoid 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);
}

--
OU
May 6 '07 #2
Jess wrote:
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<Talloc;

template<class Invoid 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 Ttemplate<class Invoid
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)

May 6 '07 #3
On May 6, 11:13 pm, Rolf Magnus <ramag...@t-online.dewrote:
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)
What's the difference between template<class T, class Invoid ....
and your example? Aren't they both template functions with two type
parameters?

Thanks!
Jess

May 6 '07 #4
Jess wrote:
On May 6, 11:13 pm, Rolf Magnus <ramag...@t-online.dewrote:
>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)

What's the difference between template<class T, class Invoid ....
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.

May 7 '07 #5
On May 7, 2:58 pm, Rolf Magnus <ramag...@t-online.dewrote:
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
May 7 '07 #6
On 7 Maj, 08:47, Jess <w...@hotmail.comwrote:
On May 7, 2:58 pm, Rolf Magnus <ramag...@t-online.dewrote:
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?
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.

--
Erik Wikström

May 7 '07 #7
On May 7, 5:32 pm, Erik Wikström <eri...@student.chalmers.sewrote:
template<class T, class Li>
void Vec<T, In>::create(In, In)
What is "Li" here? Is it an extra parameter?
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
Is this because of the outer "template<class A>"?
and then you have something else which is
parameterized by B,
Is this because of the second "template<class B>"? Then, what is that
"something else", since there's nothing left?
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

May 7 '07 #8
On 2007-05-08 01:14, Jess wrote:
On May 7, 5:32 pm, Erik Wikström <eri...@student.chalmers.sewrote:
>template<class T, class Li>
void Vec<T, In>::create(In, In)

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').
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

Is this because of the outer "template<class A>"?
Yes.
>
>and then you have something else which is parameterized by B,

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".
>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?
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:

template<class A, class B>
void func(A, B);
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.

--
Erik Wikström
May 8 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Rajesh.S | last post by:
some more info... >-----Original Message----- >I built a VC++.Net project as a dll and >included it as a reference in a c# project. >When I call a c++ function from the csharp >project I get...
3
by: Benedikt Weber | last post by:
I found the following compiler error with Microsoft Visual C++ .NET. I use different functions with return types determined by a Traits class. Function g() below works ok, but when I put the two...
5
by: dilip ranganathan | last post by:
Hi I have taken the liberty to cross-post this. It appeared on c.l.c++.m but the ICE is regarding VS.NET 7.1 C++ compiler. post follows: ==============================================...
5
by: Pedro Sousa | last post by:
Hi, I'm trying to create an template class that represent points in all possible dimensions, what I've made until now is #ifndef _POINT_HPP_ #define _POINT_HPP_ #include <vector>
4
by: infogoogle | last post by:
Hello, i'm having problems with the type of a template function: This code: class A {}; class B : A {}; template<class T B* fnull() { return 0; };
1
by: enbulldog | last post by:
I am working on a program to "encrypt" a text file supplied by the user, and writing to another file also given by the user. I am very new to programming and am having trouble passing information...
5
by: chrisstankevitz | last post by:
Hi, Q1: Is there a way to make a template function that works only for specific types which produces a compiler error if used with an invalid type? Q2: If not, how do people deal with this...
1
by: petschy | last post by:
hello, i've run into an error when qualifying a copy ctor 'explicit'. the strange thing is that i get a compiler error only if the class is a template and declare the variable as X<Zx = y....
4
by: fdmfdmfdm | last post by:
I have the following code: #include <iostream> #include <cstdlib> #include <cassert> using namespace std; template <class T> class Stack{ public: enum{DefaultStack = 10, EmptyStack = -1};
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.