Calling of template function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mickey22
    New Member
    • Feb 2007
    • 105

    Calling of template function

    Hi all,

    I have a template function implemented in test.cpp.

    test.h
    [code=cpp]
    class test
    {
    template<typena me T>
    void function1( );
    }

    test.cpp
    {
    template<typena me T>
    test::function1 ( )
    {
    statements
    }

    }[/code]
    Now I have my main program

    main.cpp
    [code=cpp]
    #include"test.h "

    main( )
    {
    test object1;
    object1.functio n1<float>( );
    }
    [/code]
    When I compile this rogram I get the linking error

    test.obj : error LNK2001: unresolved external symbol "public: void __thiscall test::function1 <float>(void) " (??$function1@M @test@@QAEXXZ)

    If at all I call this function on the topmost of main.cpp instead of in the class and call it without any object I get no errors at all.

    I am not able to identify the problem.Am I calling the template function in a wrong way?

    Could anyone help me with this?

    Thanks in advance.
    Last edited by sicarie; Aug 27 '07, 07:16 PM. Reason: Code Tags.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    First, this won't compile as you are missing a semi-colon at the end of the class declaration.
    Second, your method is private.
    Third, the return type of the method in the class is different from the return type of the method definition.
    Fourth, main() has no return type specified. The default is int and that is not supported in C++. main() returns an explicit int.
    Fifth, there are various extra braces scattered about.

    Other than that, it shoukld compile.

    Comment

    • RRick
      Recognized Expert Contributor
      • Feb 2007
      • 463

      #3
      Also, be careful of what compiler you are using. If you're using gnu g++, you'll have to put all of the code into the .h header file.

      In this case, be sure to add inline to methods defined outside of the class definition.

      Comment

      • gsi
        New Member
        • Jul 2007
        • 51

        #4
        Hi,
        I guess irrespective of the compiler used, it is not possible to seperate the interface and implementation in case when templated class or member functions are used. The source file has to have both the declaration and definition (In this case both test.h and test.cpp must be in main.cpp or test.h itself must have all the definitions and be included in main.cpp). The compiler generates code for the templates only when they are explicitly instantiated, hence it is not possible to seperately compile test.cpp and main.cpp(withou t the prototype definitions) and link them together .

        Please advice if misleading.

        Thanks,
        gsi.

        Comment

        • mickey22
          New Member
          • Feb 2007
          • 105

          #5
          the code I have wriiten just as an example..To clear the things, I have declared the function as public and main also I have specified as

          int main( )

          I am compiling test.cpp and main.cpp together at the same time.

          I am not able to undestand how to call my my template function in the main using the object..I have to give my template T as float.My guess is the way I am calling template function is something wrong in it.

          Could you please suggest any way.

          Thanks.

          Comment

          • mickey22
            New Member
            • Feb 2007
            • 105

            #6
            I just tried including the template definition inside test.h instead of in the test.cpp.I don't get any linker errors when I call in the same way using the object.
            But including the definition in the header file is not optimal solution.Is there any other way of doing this?

            Thanks

            Comment

            • sicarie
              Recognized Expert Specialist
              • Nov 2006
              • 4677

              #7
              mickey22-

              You're an established enough user to know that you need to follow the Posting Guidelines, so I'm issuing a formal warning - please conform to the Posting Guidelines from now on.

              Thanks,

              sicarie

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Originally posted by gsi
                I guess irrespective of the compiler used, it is not possible to seperate the interface and implementation in case when templated class or member functions are used.
                Separating the interface from the implementation has nothing to do with where the code is located.

                This only applies to polymorphic hierarchies. That is, base classes with virtual functions. In this case the interface is the set of public non-virtual methods. The implementation is separated because these base class functions are not overriden by derived classes. Generally, the public non-virtual base class methods call a private virtual base class method for the implementation. It is this function that is overriden by a private virtual function in the derived class.

                Remember, the base class method is the interface and the virtual base class method is the implementation. You do not want these to be the same function.

                Check out the design pattern called Template Method.

                Textbooks and teachers notwithstanding , public virtual functions in a polymorphic base class are a big no-no.

                Comment

                • RRick
                  Recognized Expert Contributor
                  • Feb 2007
                  • 463

                  #9
                  Keeping the class definition in the same file as the declaration only applies to templates and not to your typical base/derived class.

                  The GNU compiler generates all of the template code at compiler time. This is why all of the template code needs to be in the same file. Other compilers allow you to separate template code into header and source code files. Check with your compiler for the specific rules and details.

                  My understanding is that resolving templates during linkage is not a trivial process, and for sume reason GNU has decided not to tackle this problem.

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    Originally posted by RRick
                    My understanding is that resolving templates during linkage is not a trivial process, and for sume reason GNU has decided not to tackle this problem.
                    Neither has Microsoft.

                    Comment

                    Working...