namespace help

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

    namespace help

    file: prog1.cpp
    #include <stdio.h>
    #include "temp1.h"

    int main()
    {
    double d;
    Templ1::func(d) ;
    return 0;
    }

    file: temp1.h
    namespace Templ1
    {
    template <class T> void func(T &);
    }

    file:temp1.cpp
    namespace Templ1
    {
    template <class T> void func(T &i)
    {
    i = 0;
    }
    }

    When I compile using VC, I get:
    Linking...
    prog1.obj : error LNK2001: unresolved external symbol "void __cdecl
    Templ1::func(in t &)" (?func@Templ1@@ YAXAAH@Z)
    Debug/prog1.exe : fatal error LNK1120: 1 unresolved externals

    How can I still use templated version func() from different module (temp1.*)

    Regards,
    Elias


  • Russell Hanneken

    #2
    Re: namespace help

    lallous wrote:[color=blue]
    > file: temp1.h
    > namespace Templ1
    > {
    > template <class T> void func(T &);
    > }
    >
    > file:temp1.cpp
    > namespace Templ1
    > {
    > template <class T> void func(T &i)
    > {
    > i = 0;
    > }
    > }
    >
    > When I compile using VC, I get:
    > Linking...
    > prog1.obj : error LNK2001: unresolved external symbol "void __cdecl
    > Templ1::func(in t &)" (?func@Templ1@@ YAXAAH@Z)
    > Debug/prog1.exe : fatal error LNK1120: 1 unresolved externals
    >
    > How can I still use templated version func() from different module
    > (temp1.*)[/color]

    Check the FAQ:



    Regards,

    Russell Hanneken
    rghanneken@pobo x.com
    Remove the 'g' from my address to send me mail.


    Comment

    • lallous

      #3
      Re: namespace help

      "Russell Hanneken" <rghanneken@pob ox.com> wrote in message
      news:j7cpb.2388 $qh2.2054@newsr ead4.news.pas.e arthlink.net...[color=blue]
      > lallous wrote:[color=green]
      > > file: temp1.h
      > > namespace Templ1
      > > {
      > > template <class T> void func(T &);
      > > }
      > >
      > > file:temp1.cpp
      > > namespace Templ1
      > > {
      > > template <class T> void func(T &i)
      > > {
      > > i = 0;
      > > }
      > > }
      > >
      > > When I compile using VC, I get:
      > > Linking...
      > > prog1.obj : error LNK2001: unresolved external symbol "void __cdecl
      > > Templ1::func(in t &)" (?func@Templ1@@ YAXAAH@Z)
      > > Debug/prog1.exe : fatal error LNK1120: 1 unresolved externals
      > >
      > > How can I still use templated version func() from different module
      > > (temp1.*)[/color]
      >
      > Check the FAQ:
      >
      >[/color]

      2[color=blue]
      >
      > Regards,
      >
      > Russell Hanneken
      > rghanneken@pobo x.com
      > Remove the 'g' from my address to send me mail.
      >
      >[/color]
      Hello,
      So I understand that I must say: #include "temp1.cpp" instead of ".h" (or
      move implementation into header file) ?

      What alternative do you suggest, even if I have to redesign my code...
      I want to use templates and namespace...

      Currently, I put in header file different version of func() as:
      funcInt(int &r);
      funcDouble(doub le &r);

      then in cpp file I put:
      void funcInt(int &i)
      {
      func(i);
      }

      void funcDouble(doub le &i)
      {
      func(i);
      }


      Regards,
      Elias


      Comment

      • Rolf Magnus

        #4
        Re: namespace help

        lallous wrote:
        [color=blue]
        > So I understand that I must say: #include "temp1.cpp" instead of ".h"
        > (or move implementation into header file) ?[/color]

        or #include the .cpp file at the end of the header.

        Comment

        Working...