vector iterator and derived template classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ralf Goertz

    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;
    }
  • Zeppe

    #2
    Re: vector iterator and derived template classes

    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

    Comment

    • Salt_Peter

      #3
      Re: vector iterator and derived template classes

      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;
      >
      }

      Comment

      • Ralf Goertz

        #4
        Re: vector iterator and derived template classes

        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

        Comment

        • Ralf Goertz

          #5
          Re: vector iterator and derived template classes

          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

          Comment

          • puzzlecracker

            #6
            Re: vector iterator and derived template classes

            template <class Tclass Derived2: public Base<T>
            {
                 vector<Derived1 <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

            Comment

            • Bo Persson

              #7
              Re: vector iterator and derived template classes

              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



              Comment

              • Bo Persson

                #8
                Re: vector iterator and derived template classes

                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


                Comment

                Working...