Problem on template specialization

S

shuisheng

Dear All,

I have a simple code, but it has some problem in cimpiling. Would you
please help me to look at it?

template<class Type, size_t N>
class Vector
{
private:
Type data[N];

public:

template<size_t i>
Type Product() const
{
return data * Product(i+1);
}

template<>
Type Product<N>() const
{
return 1;
}
}

The compilor error is that:

Error 1 error C2975: 'i' : invalid template argument for
'Vector<Type,N>::product', expected compile-time constant expression

What I assume is that N is a compile-time constant expression. I
appreciate your help.

Bests,

Shuisheng
 
R

Rolf Magnus

shuisheng said:
Dear All,

I have a simple code, but it has some problem in cimpiling. Would you
please help me to look at it?

template<class Type, size_t N>
class Vector
{
private:
Type data[N];

public:

template<size_t i>
Type Product() const
{
return data * Product(i+1);


There is no Product() function that takes an integer as argument in your
code.
}

template<>
Type Product<N>() const
{
return 1;
}
}

The compilor error is that:

Error 1 error C2975: 'i' : invalid template argument for
'Vector<Type,N>::product', expected compile-time constant expression

Which line does that error refer to?
What I assume is that N is a compile-time constant expression.

Well, you haven't showed how you are instantiating your template.
 
S

shuisheng

Rolf Magnus 写é“:
template<class Type, size_t N>
class Vector
{
private:
Type data[N];

public:

template<size_t i>
Type Product() const
{
return data * Product(i+1);


There is no Product() function that takes an integer as argument in your
code.
}

template<>
Type Product<N>() const <<<<<<<<<<< ERROR LINE

{
return 1;
}
}

The compilor error is that:

Error 1 error C2975: 'i' : invalid template argument for
'Vector<Type,N>::product', expected compile-time constant expression

Which line does that error refer to?
What I assume is that N is a compile-time constant expression.

Well, you haven't showed how you are instantiating your template.


Vector<int, 4> v(1);
int prod = v.Product<0>();

Thanks.
 
J

Jeff Faust

You can't specialize a class function on the class template parameter.
You can't use 'N' as the specialization. I can dig up the standard-eze
if you'd like.

Jeff
 

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,005
Messages
2,570,264
Members
46,859
Latest member
HeidiAtkin

Latest Threads

Top