Newbie question about templates



 DEVELOP > c-Plus-Plus > Newbie question about templates

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Chris"
Date: 25 Jan 2008 04:33:38 PM
Object: Newbie question about templates
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
.

User: "Pascal Bourguignon"

Title: Re: Newbie question about templates 25 Jan 2008 05:26:03 PM
Chris <cnathaide@yahoo.com> writes:

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?
--
__Pascal Bourguignon__ http://www.informatimago.com/
Grace personified,
I leap into the window.
I meant to do that.
.

User: "Salt_Peter"

Title: Re: Newbie question about templates 25 Jan 2008 07:13:56 PM
On Jan 25, 5:33 pm, Chris <cnatha...@yahoo.com> wrote:

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
*/
.
User: "Chris"

Title: Re: Newbie question about templates 25 Jan 2008 09:24:32 PM
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
.
User: "Salt_Peter"

Title: Re: Newbie question about templates 25 Jan 2008 09:41:09 PM
On Jan 25, 10:24 pm, Chris <cnatha...@yahoo.com> wrote:

Thanks for your replies. Here is my complete code.

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

#include <iostream>


using namespace std;

instead of using namespace...


template<typename T>
T testTemplate(vector<T>& x1)

T testTemplate(std::vector< T >& x1)

{
return x1[0];

}

int main()
{
vector<int> v(10,9);

std::vector< int > v(10, 9);
or
using namespace std;
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

.
User: "Chris"

Title: Re: Newbie question about templates 26 Jan 2008 03:43:45 AM
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
.





  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER