Results 1 to 2 of 2

Thread: template, undefined reference to fnc

  1. #1
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default template, undefined reference to fnc

    Hi, i have a problem with compiling these file:


    Qt Code:
    1. /** @file struct.h */
    2.  
    3. #ifndef STRUCT_H
    4. #define STRUCT_H
    5.  
    6. using namespace std;
    7.  
    8. ...
    9.  
    10. template <typename cStruct> int InitLine(cStruct * toStruct);
    11. template <typename cStruct, typename cItem> int AddToLine(cStruct * toStruct, cItem * newItem);
    12. template <typename cStruct, typename cItem> int DeleteFromLine(cStruct * toStruct, cItem * delItem);
    13.  
    14. #endif // STRUCT_H
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. /** @file struct.cpp */
    2.  
    3. #include "struct.h"
    4.  
    5. template <typename cStruct> int InitLine(cStruct * toStruct)
    6. {
    7. toStruct->first=NULL;
    8. toStruct->last=NULL;
    9. toStruct->top=0;
    10.  
    11. return 0;
    12. }
    13. ...
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. /** @file mainwindow.h */
    2.  
    3. #ifndef MAINWINDOW_H
    4. #define MAINWINDOW_H
    5.  
    6. #include "struct.h"
    7.  
    8. ...
    9.  
    10. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    and when i call fnc from mainwindow.cpp
    Qt Code:
    1. InitLine(NewLine);
    To copy to clipboard, switch view to plain text mode 

    the compile output is:
    .../mainwindow.cpp:702: undefined reference to `int InitLine<TMyStruct>(TMyStruct*)'

  2. #2
    Join Date
    Jun 2007
    Location
    Massachusetts, USA
    Posts
    24
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: template, undefined reference to fnc

    A template implementation must be visible at compile time. The code you show as "struct.cpp" should be part of "struct.h", not in its own file. Then when "mainwindow" is compiled, the template will be found.

Similar Threads

  1. Undefined reference to crt
    By derektaprell in forum Installation and Deployment
    Replies: 0
    Last Post: 20th October 2009, 08:34
  2. how to add static library into qmake
    By Namrata in forum Qt Tools
    Replies: 1
    Last Post: 20th November 2007, 17:33
  3. MS Sql native driver??
    By LordQt in forum Qt Programming
    Replies: 4
    Last Post: 9th October 2007, 13:41
  4. error undefined reference ...............
    By amit_pansuria in forum Qt Programming
    Replies: 2
    Last Post: 8th June 2007, 14:28
  5. how to correctly compile threads support?
    By srhlefty in forum Installation and Deployment
    Replies: 9
    Last Post: 25th June 2006, 19:15

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.