473,396 Members | 1,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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

--
[--- PGP key FD05BED7 --- http://www.root42.de/ ---]
Dec 1 '06 #1
6 14959
Arne Schmitz wrote:
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;
};
Dec 1 '06 #2
Arne Schmitz wrote:
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<TObject *>::size_type size_type;
// 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-lit...html#faq-35.18

Cheers! --M

Dec 1 '06 #3

Nate Barney wrote:
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++.

Dec 1 '06 #4

"Nate Barney" <na********@gmail.comwrote in message
news:6N******************************@adelphia.com ...
Arne Schmitz wrote:
>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;
};
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
Dec 1 '06 #5
Cy Edmunds wrote:
>
// 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)
Dec 1 '06 #6
mlimber wrote:
>typedef std::vector<TObject *ElemVector;
typedef std::vector<TObject *>::size_type *size_type;

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

--
[--- PGP key FD05BED7 --- http://www.root42.de/ ---]
Dec 4 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: tirath | last post by:
Hi all, I have a templated class that derives from a non-templated abstract class. How do I then cast a base class pointer to a <templated> derived class pointer in a generalised fashion? ...
11
by: Scott Brady Drummonds | last post by:
Hi, everyone, I've checked a couple of on-line resources and am unable to determine how reinterpret_cast<> is different from static_cast<>. They both seem to perform a compile-time casting of...
4
by: Grey Plastic | last post by:
I have several classes that all keep track of static data. However, the manner that they keep track of static data is identical, and so I'm using the template<class Child> class Parent { ... };...
3
by: Alicia | last post by:
Hello, I am trying to figure out how to call an overloaded operator<< inherited from a base class. #ifndef PHONECALL #define PHONECALL #include "time.h" #include "interval.h"
23
by: guru.slt | last post by:
Hi, see this code: auto_ptr<int> int_auto_p(new int(3)); auto_ptr<int> int_auto_p2(int_auto_p.get()); Then, both auto_pointer will own the same object. That will violate the single...
1
by: Mike Strieder | last post by:
How can i get the text of the System.Type e.g. "base64Binary" from the .Net type "byte" I can not found any Function to give back this Schematype as string. thx for your help
4
by: matty.hall | last post by:
I have two classes: a base class (BaseClass) and a class deriving from it (DerivedClass). I have a List<DerivedClass> that for various reasons needs to be of that type, and not a List<BaseClass>....
4
by: Neil Zanella | last post by:
Hello, I would like to know what the difference is among the constructs <%= %> for evaluating an expression and displaying the evaluated result on the page and <%# %>. In particular I would like...
13
by: baltasarq | last post by:
Hi, there ! When I use dynamic_cast for a single object, I do something like: void foo(Base *base) { if ( dynamic_cast<Derived *>( base ) != NULL ) { ((Derived *) base)->do_something() } }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.