Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Storing template classes

    C++ Gurus
    2
    8
    1360
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • G
      GrahamL last edited by

      Hi
      So I have a templated class
      @
      template<typename T>
      class MyClass
      {
      ....
      }
      @

      Is it possible to have a list of these?
      I'm guessing not as this does not compile
      @
      QList<MyClass<T> > myList;
      @

      Is there a way of achieving this?

      Thanks

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        What error are you getting ?

        For example
        @QList< QVector<int> > myList;@

        Builds without any problem

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • G
          GrahamL last edited by

          Hi
          I am trying to store the templated class not the instantiations

          @
          QList<MyClass<T> > myList;
          @

          The error is T : undeclared identifier

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            Ok, that, AFAIK no you can't store a template class

            What's your use case ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply Reply Quote 0
            • G
              GrahamL last edited by

              Hi
              Thats what I thought

              I want to associate a string with a particular instantiation of the template class

              1 Reply Last reply Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                What would be the end-result ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply Reply Quote 0
                • G
                  GrahamL last edited by

                  Hi

                  I think I will have to find another way

                  Thanks anyway

                  1 Reply Last reply Reply Quote 0
                  • G
                    GrahamL last edited by

                    The solution is to derive the template from a base class
                    @
                    class Base
                    {
                    Base() {}
                    ~Base() {}
                    };

                    template<class T>
                    class MyTemplate : public Base
                    {
                    MyTemplate() {}
                    ~MyTemplate() {}
                    };

                    class MyStore
                    {
                    QList<const Base*> m_list;
                    }

                    @

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post