Newbie question about templates

C

Chris

Hi,

It have a simple question about templates. I am using the OpenWatcom
compiler. Here is some test code

#include <vector>

using namespace std;

int testInt(vector<int>& x1)
{
return x1[0];
};

template<typename T>
T testTemplate(vector<T>& x1)
{
T y = x1[0];
return y;
};

The compiler complains about the function "testTemplate": non-type
parameter supplied for a type argument.

I am not seeing the problem. I appreciate any help in this matter.

Thanks

Chris
 
P

Pascal Bourguignon

Chris said:
Hi,

It have a simple question about templates. I am using the OpenWatcom
compiler. Here is some test code

#include <vector>

using namespace std;

int testInt(vector<int>& x1)
{
return x1[0];
};

template<typename T>
T testTemplate(vector<T>& x1)
{
T y = x1[0];
return y;
};

The compiler complains about the function "testTemplate": non-type
parameter supplied for a type argument.

I am not seeing the problem. I appreciate any help in this matter.

Neither do we.

-*- mode: compilation; default-directory: "/tmp/" -*-
Compilation started at Sat Jan 26 00:25:24

cat a.c++ ; g++ -c -o a.o a.c++
#include <vector>

using namespace std;

int testInt(vector<int>& x1)
{
return x1[0];
};

template<typename T>
T testTemplate(vector<T>& x1)
{
T y = x1[0];
return y;
};

Compilation finished at Sat Jan 26 00:25:25


Perhaps you could try to provide us with the true program that gives
you errors, and avoid having us try to guess?
 
S

Salt_Peter

Hi,

It have a simple question about templates. I am using the OpenWatcom
compiler. Here is some test code

#include <vector>

using namespace std;

int testInt(vector<int>& x1)
{
return x1[0];

};

extraneous semicolon, the above is a function definition

template<typename T>
T testTemplate(vector<T>& x1)
{
T y = x1[0];
return y;

return x1[0];

no semicolon
The compiler complains about the function "testTemplate": non-type
parameter supplied for a type argument.

I am not seeing the problem. I appreciate any help in this matter.

Thanks

Chris


Templated functions don't exist until a version of the template is
instantiated or generated.
So until you supply how you are using the above template, we can't
really offer much help.

This works:

int main()
{
std::vector<int> v(10, 9);
std::cout << testTemplate(v) << std::endl;
}

/*
9
*/
 
C

Chris

Thanks for your replies. Here is my complete code.

#include <vector>
#include <iostream.h>

using namespace std;

template<typename T>
T testTemplate(vector<T>& x1)
{
return x1[0];
}

int main()
{
vector<int> v(10,9);
std::cout << testTemplate(v) << std::endl;
}

The compiler shows the following output:

CL /nologo /G5 /MDd /W4 /GX /Od /FD /D "WIN32" /D "_DEBUG" /D
"_WINDOWS" /D "_MBCS" /c ..\test.cpp

C:\WATCOM\H\vector(50): Error! E350: col(51) non-type parameter
supplied for a type argument
C:\WATCOM\H\vector(50): Note! N393: col(51) included from ..
\test.cpp(1)
...\test.cpp(15): Error! E651: col(34) cannot instantiate testTemplate
...\test.cpp(6): Note! N649: col(3) template function '?
testTemplate( std::vector<?,<error>> & )' defined in: ..\test.cpp(6)
(col 3)



Thanks for your help.

Chris
 
C

Chris

Hi,

This code compiles with the Borland compiler, but not with the
OpenWatcom compiler. I am not sure why.
I will use the Borland compiler for now.

Thanks for your help.

Regards

Chris
 

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

Similar Threads


Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,206
Latest member
SybilSchil

Latest Threads

Top