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. [Solved] Template function in seperate cpp file
Forum Updated to NodeBB v4.3 + New Features

[Solved] Template function in seperate cpp file

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.5k 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.
  • H Offline
    H Offline
    Harrix
    wrote on 9 May 2013, 09:02 last edited by
    #1

    I use Qt 5.0.1 and created simple Qt Gui Application. Then I created QtHarrixLibrary.cpp:
    @#include "QtHarrixLibrary.h"

    template <class T> T funcT(T x)
    {
    return 2*x;
    }

    int func(int x)
    {
    return 2*x;
    }
    @

    and QtHarrixLibrary.h files:
    @#ifndef QTHARRIXLIBRARY_H
    #define QTHARRIXLIBRARY_H

    template <class T> T funcT(T x);

    int func(int x);

    #endif // QTHARRIXLIBRARY_H
    @

    Then I added files in project (Add Existing Files...), added in mainwindow.cpp code:
    @#include "QtHarrixLibrary.h"@

    Then added pushButton and textEdit. And pushButton has slot:
    @void MainWindow::on_pushButton_clicked()
    {
    int x=5;

    int y=func(x);
    
    ui->textEdit->insertPlainText(QString::number(y)+"\n");
    
    y=funcT(y);
    
    
    ui->textEdit->insertPlainText(QString::number(y)+"\n");
    

    }
    @

    And I have errors:
    C:\Qt\untitled4\mainwindow.cpp:26: error: undefined reference to `int funcT<int>(int)'
    collect2.exe:-1: error: error: ld returned 1 exit status

    When I use only function func program is working. When I don't use seperate cpp file and add funcT in mainwindow.cpp program ia working.

    How fix it?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on 9 May 2013, 09:25 last edited by
      #2

      Hi,

      template functions and classes shall be known to the compiler at Compile Time not at Link Time.

      When you use templates you can

      Include template implementations in header file

      Define a external source included in header file

      Example 1
      QtHarrixLibrary.h
      @
      #ifndef QTHARRIXLIBRARY_H
      #define QTHARRIXLIBRARY_H

      template <class T> T funcT(T x)
      {
      return 2*x;
      }

      int func(int x);

      #endif // QTHARRIXLIBRARY_H
      @

      QtHarrixLibrary.cpp

      @
      #include "QtHarrixLibrary.h"

      int func (int x)
      {
      return 2*x;
      }
      @

      Example 2
      QtHarrixLibrary.h
      @
      #ifndef QTHARRIXLIBRARY_H
      #define QTHARRIXLIBRARY_H

      template <class T> T funcT(T x);

      int func(int x);

      #include "QtHarrixLibrary.tpp"
      #endif // QTHARRIXLIBRARY_H
      @

      QtHarrixLibrary.cpp
      @
      #include "QtHarrixLibrary.h"

      int func (int x)
      {
      return 2*x;
      }
      @

      QtHarrixLibrary.tpp
      @
      template <class T>
      T funcT (T x)
      {
      return 2*x;
      }
      @

      Last TIP:
      use
      @template <typename T>@

      instead of

      @template <class T>@

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • H Offline
        H Offline
        Harrix
        wrote on 11 May 2013, 09:29 last edited by
        #3

        Thank you so much! Everything works!

        1 Reply Last reply
        0

        3/3

        11 May 2013, 09:29

        • Login

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