vector iterator and derived template classes

R

Ralf Goertz

Hi,

the following templated code doesn't compile. It gives the error:

template_it.cc:17: error: type 'std::vector<Derived1<T>*,
std::allocator<Derived1<T>*> >' is not derived from type 'Derived2<T>'
template_it.cc:17: error: expected ';' before 'vi'


However, the same code compiles fine when I don't use any templates.
Also, why ist it okay to define (in class Derived2) the vector v but not
okay to define the iterator?

Thanks,

Ralf


#include <vector>

using namespace std;

template <class ID_TYPE> class Base
{
int id;
};

template <class T> class Derived1: public Base<T>
{
};

template <class T> class Derived2: public Base<T>
{
vector<Derived1<T>*> v;
vector<Derived1<T>*>::iterator vi;
};

int main()
{
return 0;
}
 
Z

Zeppe

Dear Ralf,

Ralf said:
Hi,

the following templated code doesn't compile. It gives the error:

template_it.cc:17: error: type 'std::vector<Derived1<T>*,
std::allocator<Derived1<T>*> >' is not derived from type 'Derived2<T>'
template_it.cc:17: error: expected ';' before 'vi'

because the code is ambiguous.
template <class T> class Derived2: public Base<T>
{
vector<Derived1<T>*> v;
vector<Derived1<T>*>::iterator vi;
};

change to:

template <class T> class Derived2: public Base<T>
{
vector<Derived1<T>*> v;
typename vector<Derived1<T>*>::iterator vi;
};

Best regards,

Zeppe
 
S

Salt_Peter

Hi,

the following templated code doesn't compile. It gives the error:

template_it.cc:17: error: type 'std::vector<Derived1<T>*,
std::allocator<Derived1<T>*> >' is not derived from type 'Derived2<T>'
template_it.cc:17: error: expected ';' before 'vi'

However, the same code compiles fine when I don't use any templates.
Also, why ist it okay to define (in class Derived2) the vector v but not
okay to define the iterator?

Thanks,

Ralf

#include <vector>

using namespace std;

template <class ID_TYPE> class Base
{
int id;

};

template <class T> class Derived1: public Base<T>
{

};

template <class T> class Derived2: public Base<T>
{
vector<Derived1<T>*> v;

typename std::vector said:
vector<Derived1<T>*>::iterator vi;

same as above
a) What are dependant types?
b) keeping an iterator to a vector as a member here is bad news
if that vector resizes
c) why are you storing pointers? What is RAII?
 
R

Ralf Goertz

Salt_Peter said:
On Aug 5, 5:07 am, Ralf Goertz



same as above
a) What are dependant types?

Derived1 and Derived2 are two kinds of similar objects. I have two large
vectors, one of each kind. Each object of the Derived2 vector is
dependent on very many (but not all) objects in the Derived1 vector.
b) keeping an iterator to a vector as a member here is bad news
if that vector resizes c) why are you storing pointers? What is
RAII?

I don't keep the iterator. I just used it here for illustration. I use
pointers since Derived1 is quite large and memory is an issue.
Furthermore, both vectors are filled via push_back and then they are not
touched anymore (only the objects inside the vectors are modified). So I
think I'm safe using pointers.

Ralf
 
R

Ralf Goertz

BTW, this gives an error: "expected nested-name-specifier" using g++
version 4.2.1
 
P

puzzlecracker

template <class T> class Derived2: public Base<T>
{
     vector<Derived1<T>*> v;
     typename vector<Derived1<T>*>::iterator vi;

};

Why do we need a typename here? I don't see how it can resolve to
something else.

Thanks
 
B

Bo Persson

puzzlecracker said:
Why do we need a typename here? I don't see how it can resolve to
something else.

The compiler doesn't have any special treatment for std::vector. In
general, there could be a specialization for some Ts with a different
definition.

template<class T>
class weird
{
class iterator;
};

template<>
class weird<int>
{
int iterator;
};


Now what is iterator here?

template<class T>
class use
{
typedef weird<T>::iterator fails_sometimes;
};


Bo Persson
 
B

Bo Persson

Bo said:
The compiler doesn't have any special treatment for std::vector. In
general, there could be a specialization for some Ts with a
different definition.

template<class T>
class weird
{
class iterator;
};

template<>
class weird<int>
{
int iterator;
};


Now what is iterator here?

template<class T>
class use
{
typedef weird<T>::iterator fails_sometimes;
};

Fails always of course, as it should be

typedef typename weird<T>::iterator fails_sometimes;


See how easy it is to miss this ! :)


Bo Persson
 

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

Similar Threads


Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top