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<T>
test.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 8 4089
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;
Make that:
typedef typename vector<T>::reference rt;
For why, see http://www.comeaucomputing.com/techt...ates/#typename .
};
the compiler (g++ 4.0.2) says
test.C:8: error: type std::vector<T, std::allocator<_CharT is not derived from type test<T>
test.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.
Cheers! --M
Chris Dams wrote:
>
template <class Tclass test
{ public:
typedef vector<T>::reference rt;
};
vector<T>::reference is assumed not to be a type here.
The language requires types that are dependent on the
template variable to be called out with "typename"
typedef typename vector<T>::reference rt;
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;
try:
typedef typename vector<T>::reference rt;
};
[snip]
Best
Kai-Uwe Bux
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<T>
test.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?
Thanks,
Michael
> typedef typename vector<T>::reference rt;
Thanks to the people who have given the answer! This indeed solves it.
"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
John Carson wrote:
"Michael" <mi*******@gmail.comwrote in message
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&).
This may seem rather trivial as it seems obvious that value type should
always be T and reference should always be T&. However, either one
could actually be a "proxy" class that acts like you would expect a T
or T& to act but are not actually those things. You should always use
these typedefs when you are creating your own custom container through
composition as the OP has done.
Noah Roberts wrote:
>
John Carson wrote:
>"Michael" <mi*******@gmail.comwrote in message
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&).
This may seem rather trivial as it seems obvious that value type should
always be T and reference should always be T&. However, either one
could actually be a "proxy" class that acts like you would expect a T
or T& to act but are not actually those things.
Not correct for standard containers. The standard states:
std::vector<T>::value_type is always T.
std::vector<T,Allocator>::reference is Allocator::reference unless T=bool.
and for std::deque and std::list the second statement holds even for T=bool.
Also, std::allocator<T>::reference is T&.
There also is a good reason for these rigid requirements: suppose the client
of the standard library uses a type T and has specialized a template for
that type. You clearly would want the specialization to be chosen for the
type std::vector<T>::value_type, as well.
You should always use
these typedefs when you are creating your own custom container through
composition as the OP has done.
Good practice anyway.
Best
Kai-Uwe Bux This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Stefan Höhne |
last post by:
Hi,
as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to
MS' DOT.NET - compiler.
In the 6.0 version the capacity() of the vector did not change with the
call to...
|
by: Michael |
last post by:
Righty,
1: Is there a standard library that contain matrices and complex numbers. I
need to find eigen values of a 3x3 matrix.
2: Is there a way of getting the pointer to the start of an...
|
by: Joseph Turian |
last post by:
Some function requires a vector<const foo*> argument.
How can I cast a vector<foo*> to vector<const foo*>?
Thanks!
Joseph
|
by: Bo Peng |
last post by:
Dear list,
I am using std::vector<bool> (bit_vector) to store my bit sequence. To
access the same sequence from C (to expose to a python module), I need
to know the pointer and offset of...
|
by: richard_lavoie |
last post by:
Hi,
I have something like this:
vector<floatvec1;
and I want to cast it, so I use
vector vec2<double= static_cast< vector<double(vec1);
I always become a
error: syntax error before `>'...
|
by: Numeromancer |
last post by:
From the C++-FAQ Lite:
http://www.parashift.com/c++-faq-lite/containers.html#faq-34.3
----------------------------
34.3] Is the storage for a std::vector<Tguaranteed to be contiguous? Yes.
...
|
by: Mr. K.V.B.L. |
last post by:
I want to start a map with keys but an empty vector<string>. Not sure
what the syntax is here.
Something like:
map<string, vector<string MapVector;
MapVector.insert(make_pair("string1",...
|
by: barcaroller |
last post by:
In the boost::program_options tutorial, the author included the following
code:
cout << "Input files are: "
<< vm.as< vector<string()
<< "\n";
Basically, he is trying to print a vector...
|
by: Rune Allnor |
last post by:
Hi folks.
I have a function that takes an element in a vector as
argument. The naive interface goes as
float computeSomething(const std::vector<float>& v, size_t i)
{
size_t j = i-1;
size_t...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |