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 9064959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:23:06+00:00 2026-06-16T16:23:06+00:00

Possible Duplicate: Why can templates only be implemented in the header file? I wrote

  • 0

Possible Duplicate:
Why can templates only be implemented in the header file?

I wrote a template function and the call of type int in function main:

template <class T> T max (T a, T b) {    }
int main() {
    max(1,2);
}

As most of the C++ books said, the int max(int,int) function will be generated during the compile time when the compiler meet the max(1,2).

But in another file, I wrote the declaration of the int max(int,int) and call it, but the compiler(actually the linker) caught an error said the reference of max(int,int) is not found.

extern int max(int,int);
max(1,2);    // Error:undefined reference to max(int,int)

So , what’s the wrong point, and how can I call the max(int,int) function using extern and not a header file declaration.

Many thanks.

  • 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-06-16T16:23:08+00:00Added an answer on June 16, 2026 at 4:23 pm

    This question is only answered a few thousand times. The short form is: You either have to arrange for the template definition to be visible when used such that the compiler can implicitly instantiated the function template or you have to explicitly instantiate the function template.

    Note, that the declaration extern int max(int, int); declares a non-template function max() taking two int as parameter. This reference will never be satisfied by a function template, whether it is instantiated or not.

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

Sidebar

Related Questions

Possible Duplicate: Why can templates only be implemented in the header file? I've been
Possible Duplicate: Why can templates only be implemented in the header file? When I
Possible Duplicate: Why can templates only be implemented in the header file? I have
Possible Duplicate: Why can templates only be implemented in the header file? Why should
Possible Duplicate: Why can templates only be implemented in the header file? Undefined reference
Possible Duplicate: Why can templates only be implemented in the header file? I just
Possible Duplicate: Why can templates only be implemented in the header file? I am
Possible Duplicate: Why can templates only be implemented in the header file? “Undefined symbols”
Possible Duplicate: Why can templates only be implemented in the header file? I ran
Possible Duplicate: Why can templates only be implemented in the header file? Undefined symbol

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.