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

  • Home
  • SEARCH
  • 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 6628031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:05:00+00:00 2026-05-25T22:05:00+00:00

When we make a class we declare its functions in a header files and

  • 0

When we make a class we declare its functions in a header files and define them in a source file… then the header file can be included in the main file to use the class…

But if we declare a template class in header files and define it in the .cpp file and then if we include the header file in the main(containing int main) file then why does a linker error crop up… and the error does not crop up if we included the .cpp file(containing the header file ) in the main file… any answers plz?

  • 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-25T22:05:01+00:00Added an answer on May 25, 2026 at 10:05 pm

    Templates don’t actually produce any object code at the point where the compiler reads their source code; they’re (typically) only “instantiated” when something actually uses the template. So if you define a template function in one source file, and call it from another, the code for the template function doesn’t get compiled at all: it’s not in the first object file since nothing there needed it, and it’s not in the second object file since the compiler didn’t have access to the function’s definition.

    You define template functions in header files so that in each translation unit where something calls the template function, the compiler has access to its code and can compile a copy of it specialized with the appropriate template arguments.

    Alternatively, you can use explicit instantiation: you define the template function in a .cpp file, and also tell the compiler exactly which types that it should compile the function for. This is harder to maintain, because you have to keep track of which instantiations are needed by the rest of the program. If something calls foo<float>(), but you’ve only explicitly instantiated foo<int>() and foo<char>(), you get a missing-symbol error.

    You shouldn’t #include a .cpp file from another .cpp file. Just put the template function definitions in the header together with their declarations.

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

Sidebar

Related Questions

Is it possible to make a C++ header file (.h) that declares a class,
If we make any class in VB.NET and try to access its property in
In Python is there any way to make a class, then make a second
i make a class of elements draggable like this jQuery(.drag).draggable(); jQuery(.drop).droppable({ accept: .drag, drop:
I would like to make a class where the client side (proxy) of the
I'm trying to make a class where I put a key and value into
Why does defining __getitem__ on a class make it iterable? For instance if I
Is there a way to make a class function unoverriddable? Something like Java's final
In Java (And in general) is there a way to make a class so
Is okay to break all dependencies using interfaces just to make a class testable?

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.