"Michael" <mi*******@gmail.comwrote in message
news:11**********************@74g2000cwt.googlegro ups.com
Chris Dams wrote:
>Dear all,
The following is accepted by the compiler
template <class Tclass test
{ public:
typedef vector<int>::reference rt;
};
but after the change int -T, obtaining the code fragment,
template <class Tclass test
{ public:
typedef vector<T>::reference rt;
};
the compiler (g++ 4.0.2) says
test.C:8: error: type std::vector<T, std::allocator<_CharT is not
derived from type test<Ttest.C:8: error: expected ; before rt
Can anyone explain why this happens and how I actually can use
vector<T>::reference in my class? I need it because T can also be
bool and
vector<boolhas a different implementation.
Many thanks,
Chris
Could you please explain how to understand
vector<T>::reference? What's that?
It is a typedef. Standard containers incorporate (at least) two typedefs:
value_type and reference. value_type is a typedef for the type of the
objects stored in the container (i.e., T), while reference is a typedef for
references to the type of objects stored in the container (i.e., T&). You
can use these typedefs even without using the containers, as illustrated
below, but the main purpose is of course to facilitate template programming
with the containers.
int main()
{
int x = 5;
double y = 7.6;
std::vector<int>::reference ri = x;
std::vector<double>::reference rd = y;
cout << ri << endl;
cout << rd << endl;
return 0;
}
--
John Carson