Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Storing template classes
QtWS25 Last Chance

Storing template classes

Scheduled Pinned Locked Moved C++ Gurus
8 Posts 2 Posters 1.8k Views
  • 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 Offline
    G Offline
    GrahamL
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0
      • G Offline
        G Offline
        GrahamL
        wrote on last edited by
        #3

        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
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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
          0
          • G Offline
            G Offline
            GrahamL
            wrote on last edited by
            #5

            Hi
            Thats what I thought

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

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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
              0
              • G Offline
                G Offline
                GrahamL
                wrote on last edited by
                #7

                Hi

                I think I will have to find another way

                Thanks anyway

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  GrahamL
                  wrote on last edited by
                  #8

                  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
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved