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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:11:42+00:00 2026-06-13T11:11:42+00:00

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

  • 0

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

I just came across something that I failed to understand.
I had a problem at the linking stage in the following case.

//header file
class A 
{
    template<class T>
    std::weak_ptr<T> GetSomethingFromSomeWhere(const char* Id);
};

//cpp file
template<class T>
std::weak_ptr<T> A:GetSomethingFromSomeWhere(const char* id)
{
   //A method with the right stuff inside and the right return statement
   ...
}


//Another class
class B
{
};

//main.cpp
int main ()
{
   A a;
   auto pB = a.GetSomethingFromSomeWhere<B>( "id" );
}

This didn’t compile, during linking I have something of this kind :

Undefined symbols for architecture x86_64:
“std::__1::weak_ptr A::GetComponentFromName(char const*)”, referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I fixed it by defining the template method directly in the header file.

Should I always define template method in the header ? Why ?
I am on OSX and use clang++ with XCode if that can be of any help

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-13T11:11:43+00:00Added an answer on June 13, 2026 at 11:11 am

    Template definition needs to be visible to the code using it. Otherwise linker errors will be generated.
    There are different workarounds for situations like that:

    Read This

    • 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? Why should
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? “Undefined symbols”
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 ran
Possible Duplicate: Why can templates only be implemented in the header file? Undefined symbol
Possible Duplicate: Why can templates only be implemented in the header file? I realise
Possible Duplicate: Why can templates only be implemented in the header file? I've struggled

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.