You must include the .cpp file instead of only .h file when using templates in VC++ ...?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shen

    You must include the .cpp file instead of only .h file when using templates in VC++ ...?

    I saw this conclusion in a couple of groups. For example, if you
    define a class template, in VC++ you have to merge the .h and .cpp
    files into one file and include it in other files where they have
    something to do with this template class. Otherwise you'll get Link
    errors(unresolv ed external symbol...LNK200 1/LNK2019).

    Is it still true now? Has MS Visual studio 2003 done something for it?
    or does anyone figure out a solution other than changing the .h/.cpp
    style?

    It is not good to include implementation in other files, for it tends
    to put redefinition bugs into your project and...anyway it is off the
    ..h/.cpp way.


    Thanks for the reply,


    Shen
  • David Harmon

    #2
    Re: You must include the .cpp file instead of only .h file when using templates in VC++ ...?

    On 10 Feb 2004 18:48:35 -0800 in comp.lang.c++, syh_email@yahoo .com
    (Shen) was alleged to have written:[color=blue]
    >I saw this conclusion in a couple of groups. For example, if you
    >define a class template, in VC++ you have to merge the .h and .cpp
    >files into one file and include it in other files where they have
    >something to do with this template class.[/color]

    This issue is covered in Marshall Cline's C++ FAQ. See the topic
    "[34.12] Why can't I separate the definition of my templates class from
    it's declaration and put it inside a .cpp file?" It is always good to
    check the FAQ before posting. You can get the FAQ at:


    Please don't invite an off-topic thread by crossposting to comp.lang.c++
    and product-specific groups. Pick the _one_ most relevant group to post
    to. See the welcome message posted twice per week in comp.lang.c++ or
    available at http://www.slack.net/~shiva/welcome.txt

    Comment

    • Jonathan Turkanis

      #3
      Re: You must include the .cpp file instead of only .h file when using templates in VC++ ...?

      "Shen" <syh_email@yaho o.com> wrote in message
      news:81ca23b2.0 402101848.49042 1fe@posting.goo gle.com...[color=blue]
      > I saw this conclusion in a couple of groups. For example, if you
      > define a class template, in VC++ you have to merge the .h and .cpp
      > files into one file and include it in other files where they have
      > something to do with this template class. Otherwise you'll get Link
      > errors(unresolv ed external symbol...LNK200 1/LNK2019).
      >
      > Is it still true now? Has MS Visual studio 2003 done something for[/color]
      it?[color=blue]
      > or does anyone figure out a solution other than changing the .h/.cpp
      > style?
      >
      > It is not good to include implementation in other files, for it[/color]
      tends[color=blue]
      > to put redefinition bugs into your project and...anyway it is off[/color]
      the[color=blue]
      > .h/.cpp way.
      >[/color]

      There is a language feature called export which allows you include
      function template and class template static data definitions in .cpp
      files. Last I header, microsoft had no intention ever to implement
      this feature.

      I have never used export, because of its limited availability, but I
      think you can do something like this:

      ------------------------------
      "my_template.hp p":
      ------------------------------

      #ifdef HAS_EXPORT
      #define EXPORT export
      #else
      #define EXPORT
      #endif

      EXPORT
      template<typena me T>
      struct my_template {
      void f();
      static int* g;
      };

      #ifndef HAS_EXPORT
      #include "my_template.cp p"
      #endif

      ------------------------------
      "my_template.cp p"
      ------------------------------

      #ifndef HAS_EXPORT
      #include "my_templat e.h"
      #endif

      template<typena me T>
      void my_template<T>: :f() { /* */ }

      template<typena me T>
      int* my_template<T>: :g;

      -------------------------------------

      This allows you to take advanage of export where it is available.
      (Conceivably there could be subtle difference in the meanings of
      programs depending on whether HAS_EXPORT is defined, because of
      name-lookup rules.)

      Jonathan



      Comment

      • Jonathan Turkanis

        #4
        Re: You must include the .cpp file instead of only .h file when using templates in VC++ ...?


        "Jonathan Turkanis" <technews@kanga roologic.com> wrote in:
        [color=blue]
        > files. Last I header, microsoft had no intention ever to implement[/color]

        Ahem .... last I 'heard'

        Jonathan


        Comment

        • Carl Daniel [VC++ MVP]

          #5
          Re: You must include the .cpp file instead of only .h file when using templates in VC++ ...?

          Review the thread on this very newsgroup (microsoft.publ ic.vc.stl) titled
          "vc++ 2004" for an elaborate discussion of the value and costs of export,
          which is the C++ language feature that lets you keep template
          implementations separate from template definitions. To date, Comeau is the
          only compiler vendor shipping an export-capable compiler, so MS is far from
          alone in not supporting it.

          -cd

          Shen wrote:[color=blue]
          > I saw this conclusion in a couple of groups. For example, if you
          > define a class template, in VC++ you have to merge the .h and .cpp
          > files into one file and include it in other files where they have
          > something to do with this template class. Otherwise you'll get Link
          > errors(unresolv ed external symbol...LNK200 1/LNK2019).
          >
          > Is it still true now? Has MS Visual studio 2003 done something for it?
          > or does anyone figure out a solution other than changing the .h/.cpp
          > style?
          >
          > It is not good to include implementation in other files, for it tends
          > to put redefinition bugs into your project and...anyway it is off the
          > .h/.cpp way.
          >
          >
          > Thanks for the reply,
          >
          >
          > Shen[/color]


          Comment

          • John Harrison

            #6
            Re: You must include the .cpp file instead of only .h file when using templates in VC++ ...?


            "Shen" <syh_email@yaho o.com> wrote in message
            news:81ca23b2.0 402101848.49042 1fe@posting.goo gle.com...[color=blue]
            > I saw this conclusion in a couple of groups. For example, if you
            > define a class template, in VC++ you have to merge the .h and .cpp
            > files into one file and include it in other files where they have
            > something to do with this template class. Otherwise you'll get Link
            > errors(unresolv ed external symbol...LNK200 1/LNK2019).
            >
            > Is it still true now? Has MS Visual studio 2003 done something for it?
            > or does anyone figure out a solution other than changing the .h/.cpp
            > style?
            >
            > It is not good to include implementation in other files, for it tends
            > to put redefinition bugs into your project and...anyway it is off the
            > .h/.cpp way.
            >
            >
            > Thanks for the reply,
            >
            >
            > Shen[/color]

            Simplest option is to put the template function directly in the header file,
            just like an inline function.

            john


            Comment

            • Patrik Stellmann

              #7
              Re: You must include the .cpp file instead of only .h file when usingtemplates in VC++ ...?

              >[color=blue]
              > Simplest option is to put the template function directly in the header file,
              > just like an inline function.[/color]

              Another quite common way with same effect is to put the inline and
              template functions in a .inl-file and include that file at the bottom of
              your header. Keeps your header file 'clean' from implementations ...

              Comment

              Working...