using a template in a vector - getting error C2955

G

Gil

trying to use a template in a vector, in Visual Studio 6 on Windows
2000

#include <vector>

using namespace std;

template <typename T>
class AnyValue {
T val;
public :
AnyValue(T inv)
{
val = inv;
}
};

class ValuesObject {
int count;
vector<AnyValue, allocator<AnyValue> > ivo; // error occurs here!!
public :
ValuesObject() {}
~ValuesObject() {}
int getCount()
{
return count;
}
void addValue(AnyValue av)
{
vector<AnyValue, allocator<AnyValue> >::iterator iter = ivo.end();
ivo.insert(iter, av);
}
};

I'm getting :

error C2955: 'AnyValue' : use of class template requires template
argument list

Can anyone see what I am doing wrong?

As far as C++ is concerned, is what I am doing correct? Is this a
compiler issue with Visual C++?
 
S

Sharad Kala

Gil said:
trying to use a template in a vector, in Visual Studio 6 on Windows
2000

#include <vector>

using namespace std;

template <typename T>
class AnyValue {
T val;
public :
AnyValue(T inv)
{
val = inv;
}
};

class ValuesObject {
int count;
vector<AnyValue, allocator<AnyValue> > ivo; // error occurs here!!
public :
ValuesObject() {}
~ValuesObject() {}
int getCount()
{
return count;
}
void addValue(AnyValue av)
{
vector<AnyValue, allocator<AnyValue> >::iterator iter = ivo.end();
ivo.insert(iter, av);
}
};

I'm getting :

error C2955: 'AnyValue' : use of class template requires template
argument list

AnyValue is a class template and NOT a concrete class.
Vector is also a class template and to make a concrete class out of it you need
to provide a
template parameter (some built-in type/concrete class).
Since Vector does not expect a template template parameter, AnyValue is
rejected by it.
Hence you can't pass it as an argument to vector.

Best wishes,
Sharad
 
D

David Fisher

Gil said:
#include <vector>

using namespace std;

template <typename T>
class AnyValue {
T val;
public :
AnyValue(T inv)
{
val = inv;
}
};

A small aside - better to say:

AnyValue(T inv) : val(inv)
{
}

This will cause val to just be constructed, instead of being default
constructed and then assigned the value of inv. Even better, say:

AnyValue(const T &inv) : val(inv)

This will prevent an unnecessary copy when the constructor is called.
class ValuesObject {
int count;
vector<AnyValue, allocator<AnyValue> > ivo; // error occurs here!!

You have defined AnyValue as a template class, so it needs a template
argument (eg. AnyValue<int>).

It looks like you might mean:

template <typename T>
class ValuesObject
{
vector<AnyValue<T> > ivo;
...
};

Hope this is helpful,

David F
 
S

Senthilvel Samatharman

Gil said:
trying to use a template in a vector, in Visual Studio 6 on Windows
2000

#include <vector>

using namespace std;

template <typename T>
class AnyValue {
T val;
public :
AnyValue(T inv)
{
val = inv;
}
};

class ValuesObject {
int count;
vector<AnyValue, allocator<AnyValue> > ivo; // error occurs here!!
public :
ValuesObject() {}
~ValuesObject() {}
int getCount()
{
return count;
}
void addValue(AnyValue av)
{
vector<AnyValue, allocator<AnyValue> >::iterator iter = ivo.end();
ivo.insert(iter, av);
}
};

I'm getting :

error C2955: 'AnyValue' : use of class template requires template
argument list

Can anyone see what I am doing wrong?

As far as C++ is concerned, is what I am doing correct? Is this a
compiler issue with Visual C++?

You have defined AnyValue as a template class but you have not provided the
template arguments.
All you have to do is to replace "AnyValue" in class ValuesObject something
like "AnyValue<int>".
Else if you do not want a concrete type like int make ValuesObject class
also as a template class and provide the
template argument for AnyValue.See the code below.That compiles without any
error in the Visual C++ 6.

#include <vector>

using namespace std;

template <typename T>
class AnyValue {
T val;
public :
AnyValue(T inv )
{
val = inv;
}
};

template <typename U> class ValuesObject {
int count;
vector<AnyValue<U>, allocator<AnyValue<U> > > ivo;
public :
ValuesObject() {}
~ValuesObject() {}
int getCount()
{
return count;
}
void addValue(AnyValue<U> av)
{
vector<AnyValue<U>, allocator<AnyValue<U> > >::iterator iter =
ivo.end();
ivo.insert(iter, av);
}
};
 
C

Chris Theis

Sharad Kala said:
[SNIP]
AnyValue is a class template and NOT a concrete class.
Vector is also a class template and to make a concrete class out of it you need
to provide a
template parameter (some built-in type/concrete class).
Since Vector does not expect a template template parameter, AnyValue is
rejected by it.
Hence you can't pass it as an argument to vector.

Best wishes,
Sharad

Just to elaborate on how to do it right you need to supply a template
argument. For example:

vector< AnyValue<int> > MyVec; // note the blank between the two greater
signs!

HTH
Chris
 
S

Sharad Kala

Chris Theis said:
Sharad Kala said:
[SNIP]
AnyValue is a class template and NOT a concrete class.
Vector is also a class template and to make a concrete class out of it you need
to provide a
template parameter (some built-in type/concrete class).
Since Vector does not expect a template template parameter, AnyValue is
rejected by it.
Hence you can't pass it as an argument to vector.

Best wishes,
Sharad

Just to elaborate on how to do it right you need to supply a template
argument. For example:

vector< AnyValue<int> > MyVec; // note the blank between the two greater
signs!

Let me also elaborate again :)
You need the space between the two greater-than signs because of a compiler
property called as "Maximal Munch" i.e.
consume as many tokens as possible. When compiler sees the two >> signs it
interprets it as the shift operator hence the need for the
space as Chris pointed out.

Best wishes,
Sharad
 
C

Chris Theis

Sharad Kala said:
[SNIP]
Let me also elaborate again :)
You need the space between the two greater-than signs because of a compiler
property called as "Maximal Munch" i.e.
consume as many tokens as possible. When compiler sees the two >> signs it
interprets it as the shift operator hence the need for the
space as Chris pointed out.

Best wishes,
Sharad

BTW there is talk that future compilers will parse such a statement
correctly even with the blank missing. Though, todays compilers will
certainly have a problem with this.

Cheers
Chris
 
S

Sharad Kala

Chris Theis said:
Sharad Kala said:
[SNIP]
Let me also elaborate again :)
You need the space between the two greater-than signs because of a compiler
property called as "Maximal Munch" i.e.
consume as many tokens as possible. When compiler sees the two >> signs it
interprets it as the shift operator hence the need for the
space as Chris pointed out.

Best wishes,
Sharad

BTW there is talk that future compilers will parse such a statement
correctly even with the blank missing. Though, todays compilers will
certainly have a problem with this.

Absolutely true :)
In fact I read that C++ Standard Committee will take care of it in the next
revision of the standard.

Best wishes,
Sharad
 

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

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,416
Latest member
LionelQ387

Latest Threads

Top