|
-
December 5th, 2002, 06:07 PM
#1
template, iterator and vector
I try to have the following code running :
Code:
#include <iostream>
#include <vector>
using namespace std;
template <class T> class Point {
public:
Point(const T &_lat=0, const T &_long=0) : lat(_lat), lon(_long) {};
Point(const Point<T> &P): lat(P.getLat()), lon(P.getLon()) {};
const T& getLat() const {return lat;};
const T& getLon() const {return lon;};
private:
T lat;
T lon;
};
template <class T> class Chaine : public vector<Point<T> > {
public:
explicit Chaine(const Point<T> &P) {
this->push_back(P);
};
void display(void) {
vector<Point< class T> >::iterator it;
for (it=this->begin(); it!=this->end(); it++) {
cout << it->getLat() << " " << it->getLon() << endl;
}
};
};
int main() {
const Point<int> P(5, 3);
Chaine<int> ch(P);
ch.display();
return 0;
}
I have a compilation error at the declaration of the iterator it.
It seems to be because of the 'T' in the declaration, since a vector <Point<int>>::iterator it; works perfectly (but doesn't do the trick).
I tried to compile with the DinkumWare library, but the error message is that "template parameter t may not be used in an elaborated type specifier".
I can't remember such a limitation being mentionned in my STL and C++ books, so what's happening ? Is my code just flawed, or is it a flaw in the compiler/library ?
-
December 5th, 2002, 06:12 PM
#2
You won't believe how close you were:
remove 'class':
vector<Point<T> >::iterator it;
Jeff
-
December 5th, 2002, 06:15 PM
#3
One more 'Point':
You shouldn't inherit from std::vector<> nor from any other STL classes. They aren't built for that. For instance, there is no virtual destructor. Use containment instead.
Jeff
-
December 5th, 2002, 06:19 PM
#4
Well, I did try to remove the 'class' (in fact it was my first try), but I got another error message :
Code:
expected a ";"
vector<point<t> >::iterator it;
^
On the other hand, you're right, I forgot about the virtual destructor not being there. I'll guess I'll fall back on a containing class.
Thanks
-
December 5th, 2002, 06:30 PM
#5
It looks fine to me, given that you meant T and Point instead of t and point.
In this example, since you are inheriting, you should be able to simply use 'iterator', since it's defined publicly in the base class. When you switch to containment, I'd advise you to add a similar typedef to your public definition:
Code:
template<typename T>
class Chaine
{
public:
typedef std::vector<Point<T> >::iterator iterator;
private:
std::vector<Point<T> > m_ptVector;
};
Then you can add nice begin() and end() methods and use your class with STL algorithms.
Jeff
-
December 5th, 2002, 06:47 PM
#6
Just checked it on a standard Visual V6 (SP4) newly installed.
It works perfectly (after correcting the class error you mentioned), even when I remove all mention to "this" and the iterator type declaration.
On the other hand, since I foresee some inheritance from the Chaine object and I do not want to beg for memory leaks, I'll turn to the containment schema.
Thanks for the additional advices
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
On-Demand Webinars (sponsored)
|