473,729 Members | 2,234 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::uninitiali zed_copy(i,j,da ta);
}

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::uninitiali zed_copy(i,j,da ta);
}

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 5313
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::uninitiali zed_copy(i,j,da ta);
}
template<class T>
template<class In>
void Vec<T>::create( In i, In j){
data = alloc.allocate( j - i);
limit = avail = std::uninitiali zed_copy(i,j,da ta);
}

--
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::uninitiali zed_copy(i,j,da ta);
}

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<cla ss T>
template<cla ss 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<c lass T>" covers the class "Vec<T>" and the
second "template<c lass 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.c omwrote:
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<c lass T>" covers the class "Vec<T>" and the
second "template<c lass 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.sewro te:
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<c lass A>"?
and then you have something else which is
parameterized by B,
Is this because of the second "template<c lass 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.sewro te:
>template<cla ss 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
parameterize d with A

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

Is this because of the second "template<c lass 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
parameterize d 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
1975
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 internal compiler error. > >The function in the c++ dll got the signature >void __gc *CInitialContext::Lookup(char *)
3
2133
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 declarations f1() and f2(), the compiler gets disturbed. The error message does not even reproduce the original code correctly. When the two declarations are switched, the problem goes away. Benedikt
5
1674
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: ============================================== John Torjo wrote: >
5
1901
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
2292
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
2220
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 from file to program and back to file. I am close, but I have comiler errors. Here is my code so far: #include <iostream> #include <cctype> #include <fstream> #include <iomanip> using namespace std; //Function: displayHeader
5
2366
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 issue? I believe A1 is no. I tried below with template function "f". For A2, functors work but have ugly syntax and runtime overhead. A
1
2320
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. X<Zx(y) is fine. Tested with gcc 2.95, 3.3, 4.1, all gave the same error: t.cpp: In function 'int main()': t.cpp:44: error: no matching function for call to 'D<int>::D(D<int>&)'
4
774
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};
0
8913
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8144
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.