link error if template function is defined in non-header file

Discussion in 'C++' started by Chandra Shekhar Kumar, Jun 24, 2003.

  1. coz, compiler can't see the definition of the function f.
    so u shud include the definition of f in the header file, this is the
    practice for all the template functions

    Suzanne wrote:

    > Hi++,
    >
    > I get a link error when I try to call a template function that is
    > declared in a header file and defined in a non-header file. I do *not*
    > get this link error if I define the template function directly in the
    > header file, or if the change the function to non-template.
    >
    > For example...
    > *** Why am I getting a link error for template function f() in the code
    > below?
    >
    > Thanks!
    > Suzanne
    >
    > -----------------------------------
    > // File: Main.cpp
    >
    > #include "Templates.h"
    >
    > int main(int argc, char** argv) {
    > f<int>(5); // <--link error here
    > }
    > -----------------------------------
    > // File: Templates.h
    >
    > #ifndef TEMPLATES_H
    > #define TEMPLATES_H
    >
    > #ifndef _STD_USING
    > #define _STD_USING // Must be #define'd in order to include iostream.h.
    > #endif // _STD_USING
    >
    > #include <iostream>
    >
    > template<class T>
    > void f(T n);
    >
    > #endif // TEMPLATES_H
    > -----------------------------------
    > // File: Templates.cpp
    >
    > #include "Templates.h"
    >
    > template<class T>
    > void f(T n) {
    > std::cout << "I got an \"n\"!\n";
    > }
    Chandra Shekhar Kumar, Jun 24, 2003
    #1
    1. Advertising

  2. Chandra Shekhar Kumar

    Suzanne Guest

    Hi++,

    I get a link error when I try to call a template function that is
    declared in a header file and defined in a non-header file. I do *not*
    get this link error if I define the template function directly in the
    header file, or if the change the function to non-template.

    For example...
    *** Why am I getting a link error for template function f() in the code
    below?

    Thanks!
    Suzanne

    -----------------------------------
    // File: Main.cpp

    #include "Templates.h"

    int main(int argc, char** argv) {
    f<int>(5); // <--link error here
    }
    -----------------------------------
    // File: Templates.h

    #ifndef TEMPLATES_H
    #define TEMPLATES_H

    #ifndef _STD_USING
    #define _STD_USING // Must be #define'd in order to include iostream.h.
    #endif // _STD_USING

    #include <iostream>

    template<class T>
    void f(T n);

    #endif // TEMPLATES_H
    -----------------------------------
    // File: Templates.cpp

    #include "Templates.h"

    template<class T>
    void f(T n) {
    std::cout << "I got an \"n\"!\n";
    }
    Suzanne, Jun 24, 2003
    #2
    1. Advertising

  3. Hi Suzanne,

    Template functions are only created if you "use" them. Nothing is happening
    if you do not create an instance of a special type. Anyway, how should this
    be possible?
    This is exactly the case when your compiler creates the object file of your
    cpp. So you have an object file with nothing inside.

    Patrick

    "Suzanne" <> wrote in message
    news:3ef87563$...
    > Hi++,
    >
    > I get a link error when I try to call a template function that is
    > declared in a header file and defined in a non-header file. I do *not*
    > get this link error if I define the template function directly in the
    > header file, or if the change the function to non-template.
    >
    > For example...
    > *** Why am I getting a link error for template function f() in the code
    > below?
    >
    > Thanks!
    > Suzanne
    >
    > -----------------------------------
    > // File: Main.cpp
    >
    > #include "Templates.h"
    >
    > int main(int argc, char** argv) {
    > f<int>(5); // <--link error here
    > }
    > -----------------------------------
    > // File: Templates.h
    >
    > #ifndef TEMPLATES_H
    > #define TEMPLATES_H
    >
    > #ifndef _STD_USING
    > #define _STD_USING // Must be #define'd in order to include iostream.h.
    > #endif // _STD_USING
    >
    > #include <iostream>
    >
    > template<class T>
    > void f(T n);
    >
    > #endif // TEMPLATES_H
    > -----------------------------------
    > // File: Templates.cpp
    >
    > #include "Templates.h"
    >
    > template<class T>
    > void f(T n) {
    > std::cout << "I got an \"n\"!\n";
    > }
    >
    Patrick Kowalzick, Jun 24, 2003
    #3
  4. "Suzanne" <> wrote...
    > I get a link error when I try to call a template function that is
    > declared in a header file and defined in a non-header file. I do *not*
    > get this link error if I define the template function directly in the
    > header file, or if the change the function to non-template.
    >
    > For example...
    > *** Why am I getting a link error for template function f() in the code
    > below?


    Because the compiler doesn't get a chance to create the correct,
    required, instantiation of the template if it doesn't see the body
    of the function template when compiling the code that uses it.

    Victor
    Victor Bazarov, Jun 24, 2003
    #4
  5. Suzanne <> wrote in message news:<3ef87563$>...
    > Hi++,
    >
    > I get a link error when I try to call a template function that is
    > declared in a header file and defined in a non-header file. I do *not*
    > get this link error if I define the template function directly in the
    > header file, or if the change the function to non-template.


    Theoretically, because you didn't include the keyword 'export' in the
    declaration. That tells the compiler to defer template instantiation
    to the linker.

    In practice, most linkers can't do this yet and you need the definition
    inlined in your header.

    HTH,
    --
    Michiel Salters
    Michiel Salters, Jun 25, 2003
    #5
  6. Re: [OT gripe...] link error if template function is defined in non-headerfile

    Chandra Shekhar Kumar wrote:

    > coz, compiler can't see the definition of the function f.
    > so u shud include the definition of f in the header file, this is the
    > practice for all the template functions


    I'm sorry if I seem like a real ass for saying this, but these
    contractions are really getting up my nose for some reason. Is it
    really that hard to type the word 'you' that you absolutely must reduce
    it to 'u'? Same question for 'should' and 'shud'. Is your shift key
    broken?

    I don't know if this bugs anyone else. I've always thought of
    programmers as being careful people who pay plenty of attention to case
    and so on.

    Ah hell, maybe you *did* learn English from an IRC channel. *shrug*

    --
    Corey Murtagh
    The Electric Monk
    "Quidquid latine dictum sit, altum viditur!"
    Corey Murtagh, Jun 25, 2003
    #6
  7. Chandra Shekhar Kumar

    Gavin Deane Guest

    Re: [OT gripe...] link error if template function is defined in non-header file

    Corey Murtagh <> wrote in message news:<>...
    > Chandra Shekhar Kumar wrote:
    >
    > > coz, compiler can't see the definition of the function f.
    > > so u shud include the definition of f in the header file, this is the
    > > practice for all the template functions

    >
    > I'm sorry if I seem like a real ass for saying this, but these
    > contractions are really getting up my nose for some reason. Is it
    > really that hard to type the word 'you' that you absolutely must reduce
    > it to 'u'? Same question for 'should' and 'shud'. Is your shift key
    > broken?
    >
    > I don't know if this bugs anyone else.


    I'm in.
    Gavin Deane, Jun 26, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert Allan Schwartz
    Replies:
    1
    Views:
    394
    Victor Bazarov
    Aug 9, 2004
  2. Oodini
    Replies:
    1
    Views:
    1,762
    Keith Thompson
    Sep 27, 2005
  3. mlt
    Replies:
    2
    Views:
    831
    Jean-Marc Bourguet
    Jan 31, 2009
  4. nguillot
    Replies:
    5
    Views:
    527
  5. Alok
    Replies:
    3
    Views:
    246
Loading...

Share This Page