473,804 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

vector iterator and derived template classes

Hi,

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

template_it.cc: 17: error: type 'std::vector<De rived1<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_TYPEclass Base
{
int id;
};

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

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

int main()
{
return 0;
}
Aug 5 '08 #1
7 2683
Dear Ralf,

Ralf Goertz wrote:
Hi,

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

template_it.cc: 17: error: type 'std::vector<De rived1<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 Tclass Derived2: public Base<T>
{
vector<Derived1 <T>*v;
vector<Derived1 <T>*>::iterat or vi;
};
change to:

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

Best regards,

Zeppe
Aug 5 '08 #2
On Aug 5, 5:07 am, Ralf Goertz
<r_goe...@expir es-2006-11-30.arcornews.de wrote:
Hi,

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

template_it.cc: 17: error: type 'std::vector<De rived1<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_TYPEclass Base
{
int id;

};

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

};

template <class Tclass Derived2: public Base<T>
{
vector<Derived1 <T>*v;
typename std::vector< Derived1<T>* v;
vector<Derived1 <T>*>::iterat or 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?
>
};

int main()
{
return 0;

}
Aug 5 '08 #3
Salt_Peter wrote:
On Aug 5, 5:07 am, Ralf Goertz
>>
template <class Tclass Derived2: public Base<T>
{
vector<Derived1 <T>*v;

typename std::vector< Derived1<T>* v;
> vector<Derived1 <T>*>::iterat or vi;

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
Aug 5 '08 #4
Ralf Goertz wrote:
Salt_Peter wrote:
>On Aug 5, 5:07 am, Ralf Goertz
>>>
template <class Tclass Derived2: public Base<T>
{
vector<Derived1 <T>*v;

typename std::vector< Derived1<T>* v;
BTW, this gives an error: "expected nested-name-specifier" using g++
version 4.2.1
Aug 5 '08 #5
template <class Tclass Derived2: public Base<T>
{
* * *vector<Derived 1<T>*v;
* * *typename vector<Derived1 <T>*>::iterat or vi;

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

Thanks
Aug 5 '08 #6
puzzlecracker wrote:
>template <class Tclass Derived2: public Base<T>
{
vector<Derived 1<T>*v;
typename vector<Derived1 <T>*>::iterat or vi;

};

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>::itera tor fails_sometimes ;
};
Bo Persson

Aug 5 '08 #7
Bo Persson wrote:
puzzlecracker wrote:
>>template <class Tclass Derived2: public Base<T>
{
vector<Derive d1<T>*v;
typename vector<Derived1 <T>*>::iterat or vi;

};

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>::itera tor fails_sometimes ;
};
Fails always of course, as it should be

typedef typename weird<T>::itera tor fails_sometimes ;
See how easy it is to miss this ! :-)
Bo Persson
Aug 5 '08 #8

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

Similar topics

7
4736
by: Frank Bormann | last post by:
Hi guys, I'm a C++ beginner. I was wondering if there is a generalized vector base class, through which I could pass a pointer to any kind of vector to a function, regardless of what type of objects the passed vector is holding. Regards, Frank
3
2033
by: Ken Cecka | last post by:
This is a contrived example to demonstrate a syntax problem I'm struggling with: #include <vector> template <typename T> class Class { };
17
3362
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling this uovec at the moment (for Unit-Offset VECtor). I want the class to respond correctly to all usage of STL containers and algorithms so that it is a transparent replacement for std:vector. The options seems to be:
8
18602
by: Manuel | last post by:
Hi! If I've a vector filled with abstract classes, can I push in it the derived classes too? Even if derived classes have new methods? I've done some experiments, and it seem I can push the derived classes, but I can use them only calling the methods declared in abstract class (the new methods declared only in derived classes return an error). Please take a look to my code (hey, this is not an homework!
24
2964
by: toton | last post by:
Hi, I want to have a vector like class with some additional functionality (cosmetic one). So can I inherit a vector class to add the addition function like, CorresVector : public vector<Corres>{ public: void addCorres(Corres& c); //it do little more than push_back function. }
16
2407
by: call_me_anything | last post by:
why is the following not allowed : vector <Base *vec_of_base; vector <Derived *vec_of_derived; vec_of_base = vec_of_derived; Note : The following is allowed :
7
2537
by: Thomas | last post by:
I am compiling with g++ the fol. class: template<typename E> class C_vector_ : public std::vector<E> { private:
4
5332
by: propokergrad | last post by:
Hello, say I have two classes: class Base{...}; class Derived : public Base{...} I would like to do something similar to this: std::vector<Base*>::iterator b; std::vector<Derived*>::iterator d;
9
1736
by: Julian | last post by:
Hi, I have a vector defined like this: std::vector<Load*LoadList; which is populated with different objects of classes that are derived from 'Load'. I need to make a copy of this list during runtime. I imagine I'd have to loop through the list, make a copy of each of the objects in the vector using the copy constructor... but I don't know the class name during run time so that I can call the appropriate copy constructor.
0
9711
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9593
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10595
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9169
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.