Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Do not see template class constructor. Why?
Forum Updated to NodeBB v4.3 + New Features

Do not see template class constructor. Why?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.7k Views 1 Watching
  • 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.
  • B Offline
    B Offline
    bogong
    wrote on 19 Jan 2020, 12:09 last edited by
    #1

    Hello all!
    I've got stacked in very simple issue, need fresh eye.
    I am trying to implement template class into application in separate manner - header and cpp-body in separate files and got issue:

    Undefined symbols for architecture x86_64:
      "AStack<int>::AStack()", referenced from:
          _main in main.o
    ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    make: *** [DemoVector] Error 1
    

    When I am using it in plain main.cpp - everything works fine:

    Screenshot 2020-01-19 at 15.07.03.png

    But when I am using it in separate files - it's failing

    Screenshot 2020-01-19 at 15.08.47.png

    What am I missing?

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bogong
      wrote on 19 Jan 2020, 12:21 last edited by
      #2

      Issue closed. Solution found https://www.codeproject.com/Articles/48575/How-to-define-a-template-class-in-a-h-file-and-imp

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on 19 Jan 2020, 12:24 last edited by Chris Kawa
        #3

        Templates are instantiated at the place they are used, meaning that the body needs to be available to the compiler at that place. In short this means you can't define the template in .cpp if you're gonna use it outside.

        That's why it works in your .cpp file - declaration and definition are in the same file where it's used. When you split it to .h and .cpp only .h file gets included in the other files so the body of the template is not available.

        In short - put templates bodies in the .h file. If you want to split it a bit a common practice is to put the body in another header (often with some file name postfix or a different extension e.g. .inl) and include it at the bottom of the header where your template declaration is.

        EDIT: The methods described in that link are not very good. The third one is the closest to being good, but I would strongly advise against including .cpp file in the header. Build systems usually generate rules to compile only .cpp files meaning this will result in your .cpp being parsed and compiled unnecessarily. Just give it a .h or some other extension.

        B 1 Reply Last reply 19 Jan 2020, 12:38
        3
        • C Chris Kawa
          19 Jan 2020, 12:24

          Templates are instantiated at the place they are used, meaning that the body needs to be available to the compiler at that place. In short this means you can't define the template in .cpp if you're gonna use it outside.

          That's why it works in your .cpp file - declaration and definition are in the same file where it's used. When you split it to .h and .cpp only .h file gets included in the other files so the body of the template is not available.

          In short - put templates bodies in the .h file. If you want to split it a bit a common practice is to put the body in another header (often with some file name postfix or a different extension e.g. .inl) and include it at the bottom of the header where your template declaration is.

          EDIT: The methods described in that link are not very good. The third one is the closest to being good, but I would strongly advise against including .cpp file in the header. Build systems usually generate rules to compile only .cpp files meaning this will result in your .cpp being parsed and compiled unnecessarily. Just give it a .h or some other extension.

          B Offline
          B Offline
          bogong
          wrote on 19 Jan 2020, 12:38 last edited by
          #4

          @Chris-Kawa Got it thx. I've not been using a lot the templates last 3 years. Totally forgot it.

          1 Reply Last reply
          0

          4/4

          19 Jan 2020, 12:38

          • Login

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