Type 'foo' is not derived from type 'bar<class C>'

A

Arne Schmitz

Why do I get the following error:

error: type 'std::vector<TObject*, std::allocator<TObject*> >' is not
derived from type 'bar<TObject>'

with the following code:

#include <vector>

template<class TObject>
class bar
{

public:

typedef std::vector<TObject *> ElemVector;
typedef std::vector<TObject *>::size_type size_type;
// I even wanted to write:
// typedef ElemVector::size_type size_type;
};

I guess this is something obvious?

Arne
 
N

Nate Barney

Arne said:
Why do I get the following error:

error: type 'std::vector<TObject*, std::allocator<TObject*> >' is not
derived from type 'bar<TObject>'

with the following code:

#include <vector>

template<class TObject>
class bar
{

public:

typedef std::vector<TObject *> ElemVector;
typedef std::vector<TObject *>::size_type size_type;
// I even wanted to write:
// typedef ElemVector::size_type size_type;
};

I guess this is something obvious?

bar<TObject>::ElemVector is a typedef for a dependent type. That is,
the type of ElemVector depends on the type of TObject. Therefore, when
you do ElemVector::size_type, the compiler doesn't know that size_type
is a type. You have to tell it with 'typename', like this:

#include <vector>

template<class TObject>
class bar
{

public:

typedef std::vector<TObject *> ElemVector;
typedef typename ElemVector::size_type size_type;
};
 
M

mlimber

Arne said:
Why do I get the following error:

error: type 'std::vector<TObject*, std::allocator<TObject*> >' is not
derived from type 'bar<TObject>'

with the following code:

#include <vector>

template<class TObject>
class bar
{

public:

typedef std::vector<TObject *> ElemVector;
typedef std::vector<TObject *>::size_type size_type;

Make that:

typedef typename std::vector said:
// I even wanted to write:
// typedef ElemVector::size_type size_type;
};

I guess this is something obvious?

Not so obvious, but see this FAQ:

http://www.parashift.com/c++-faq-lite/templates.html#faq-35.18

Cheers! --M
 
N

Noah Roberts

Nate said:
bar<TObject>::ElemVector is a typedef for a dependent type. That is,
the type of ElemVector depends on the type of TObject. Therefore, when
you do ElemVector::size_type, the compiler doesn't know that size_type
is a type.

And that always results in the oddest of errors. So far, the only
compiler I have worked with that informed you that you "need typename"
is g++.
 
C

Cy Edmunds

Nate Barney said:
bar<TObject>::ElemVector is a typedef for a dependent type. That is, the
type of ElemVector depends on the type of TObject. Therefore, when you do
ElemVector::size_type, the compiler doesn't know that size_type is a type.
You have to tell it with 'typename', like this:

#include <vector>

template<class TObject>
class bar
{

public:

typedef std::vector<TObject *> ElemVector;
typedef typename ElemVector::size_type size_type;
};

You're right of course but this certainly seems like a deficiency in the
language:

// to compiler: ElemVector is a type
typedef std::vector<TObject *> ElemVector;

// to compiler: ElemVector is STILL a type
typedef typename ElemVector::size_type size_type;

doh
 
P

Pete Becker

Cy said:
// to compiler: ElemVector is a type
typedef std::vector<TObject *> ElemVector;

// to compiler: ElemVector is STILL a type
typedef typename ElemVector::size_type size_type;

That's not the issue. The typename refers to size_type, not to
ElemVector. The problem is that vector can be specialized for TObject*,
and the specialization might not provide size_type. So you have to tell
the compiler that you really mean it, and that it's an error if
vector<TObject*> doesn't have a type named size_type.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
A

Arne Schmitz

mlimber said:
Make that:

typedef typename std::vector<TObject *>::size_type  size_type;

Thanks to everyone who answered. I *always* fall for this mistake. This time
it looks a bit different, but it is the same nevertheless... I guess I
still have to meditate over the semantics still a while.

Arne
 

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
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top