PHP Warning: array_search() expects parameter 2 to be array, null given in /opt/apache/dms/b2b/_common/icom_includes/quad/quad_taxonomy/quad_taxonomy.php on line 446

PHP Warning: array_keys() expects parameter 1 to be array, null given in /opt/apache/dms/b2b/_common/icom_includes/quad/quad_taxonomy/quad_taxonomy.php on line 455

PHP Warning: implode(): Invalid arguments passed in /opt/apache/dms/b2b/_common/icom_includes/quad/quad_taxonomy/quad_taxonomy.php on line 455

PHP Warning: array_keys() expects parameter 1 to be array, null given in /opt/apache/dms/b2b/_common/icom_includes/quad/quad_taxonomy/quad_taxonomy.php on line 458

PHP Warning: implode(): Invalid arguments passed in /opt/apache/dms/b2b/_common/icom_includes/quad/quad_taxonomy/quad_taxonomy.php on line 458
template, iterator and vector
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: template, iterator and vector

  1. #1
    Join Date
    Dec 2002
    Posts
    5

    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 ?

  2. #2
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    You won't believe how close you were:

    remove 'class':

    vector<Point<T> >::iterator it;

    Jeff

  3. #3
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    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

  4. #4
    Join Date
    Dec 2002
    Posts
    5
    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

  5. #5
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    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

  6. #6
    Join Date
    Dec 2002
    Posts
    5
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)