Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6638585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:27:11+00:00 2026-05-25T23:27:11+00:00

I have a class Base in base.h , which has a template function class

  • 0

I have a class Base in base.h, which has a template function

class Base {
 template <typename T> void test(T a);
}

this template is supposed to read in int or double type, and I have class Derived, which is derived from class Base

I tried to call function test in class Derived, but I have the linker error.

In the end, I realised that if in base.cpp, I add

void test(int a);
void test(double a);

there will be no compiler error. This solution seems awkward, is there a better solution? Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:27:12+00:00Added an answer on May 25, 2026 at 11:27 pm

    C++ templates must be defined (given a complete function body) in the same translation unit (.CPP file plus all included header files) where they are used. In your header file, all you have done is declared (given the name and signature of) the function. The result is that when you include base.h, all the compiler sees is:

    class Base {
      template <typename T> void test(T a);
    }
    

    This declares but does not define the function. To define it, you must include a function body:

    class Base {
      template <typename T> void test(T a)
      {
        // do something cool with a here
      }
    }
    

    The reason why this is required is that the C++ compiler generates code for templates on an “as-needed” basis. For example, if you call:

    Base obj;
    obj.test< int >( 1 );
    obj.test< char >( 'c' );
    

    The compiler will generate two sets of machine code based on the Base::test template, one for an int and one for a char. The limitation here is that the definition of the Base::test template must be in the same translation unit (.CPP file), or else the compiler will not know how to build the machine code for each version of the Base::test function. The compiler only operates on one translation unit at a time, so it has no idea whether or not you’ve defined Base::test< T > in some other CPP file. It can only work with what it has at hand.

    This is quite different from the way generics work in C#, Java, and similar languages. Personally I like to think of templates as a text macro that gets expanded by the compiler as needed. That forces me to keep in mind that the complete body of the template function needs to be included in any CPP file where it is used.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a base class Foo that has an Update() function, which I want
I know it sounds awefully confusing, I have a base template class which has
I have a base class which has a nested type, inside. There's a function
I have a base class called Component which has a number of classes derived
I have a class which has some methods like follows (And more): template<class T>
I have a base class which has a method for moving files to appropriate
I have a base class which is non-generic with a derived generic class. The
I have a base class in which I want to specify the methods a
I have a base class Node which contains a list of child nodes. Node
I have an abstract base class which acts as an interface. I have two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.