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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:36:43+00:00 2026-05-25T12:36:43+00:00

I’m getting a linker error when using a template class where I tried implementing

  • 0

I’m getting a linker error when using a template class where I tried implementing the copy-and-swap idiom as suggested here:

What is the copy-and-swap idiom?

The template class, let’s call it “TemplateClass” is partially defined like this:

template< class T >
class TemplateClass
{
    // ...
    TemplateClass< T >& operator= ( TemplateClass< T > other );
    friend void swap( TemplateClass< T >& first, TemplateClass< T >& second );
    // ...
};

I’ve put the implementations in a separate TemplateClass.cpp which is included in the .h file.
(Edit: I have the same issue if everything is in the .h file)

The assignment operator is defined as:

template< class T >
TemplateClass< T >& TemplateClass< T >::operator= ( TemplateClass< T > other )
{
    // copy-and-swap idiom
    swap( *this, other );
    return *this;
}

and the swap method is defined as:

template< class T >
void swap( TemplateClass< T >& first, TemplateClass< T >& second )
{
    using namespace std;
    swap( first.member1, second.member1 );
    swap( first.member2, second.member2 );
    // ...
}

(Don’t worry, I do not really name my members “member1” etc)

I have a similar class which is defined in the same way, but is not a template class. Everything works fine there.
However, if I have a class TestClass which has a member TemplateClass< HandledClass > member and I make a call in one of its methods like

void TestClass::setMember( TemplateClass< HandledClass > newObject )
{
    member = newObject;
}

I get an unresolved external error:

LNK2019: Unresolved external symbol “void __cdecl swap( class TemplateClass &, class TemplateClass &)” (…) in function “public: class TemplateClass X & __thiscall TemplateClass X::operator=(class TemplateClass)” (…) in TestClass.obj

Or in other words:
Something in TestClass calls TemplateClass<HandledClass>::operator= which does not find void swap( TemplateClass<HandledClass>, TemplateClass<HandledClass> ).

So my question is:
Why does the operator not find the swap method?

It looks like it was not compiled for the template argument . Is it somehow possible to have the compiler compile friend voids as well?

I could probably ditch the friend void approach and define an in-class swap method plus an out-of-class swap method plus one in std namespace, but I don’t know if it would work that way and I’d like to avoid that if possible anyway.


Solution:

this did the job:

template< class t >
class TemplateClass
{
    friend void swap( TemplateClass& first, TemplateClass& second )
    {
        // ...
    }
};

Note how i had to remove the < T > occurrences as well.

  • 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-25T12:36:43+00:00Added an answer on May 25, 2026 at 12:36 pm

    This is a common problem when befriending non-member functions with templates. The friend declaration inside the TemplateClass does not befriend your swap template, but rather a non-templated free function swap that takes TemplateClass<T> for which ever T the template is instantiated (i.e. the specialization TemplateClass<int> will befriend a free function void swap( TemplateClass<int>&,TemplateClass<int>& ); that is not templated).

    The best solution is to provide the swap definition inlined inside the class template definition, as that will make the compiler generate a non-templated swap function for the exact type whenever needed. As another positive side effect, that swap function will only be found during Argument Dependent Lookup, so it will not take part of overload resolution for anything that does not involve your template.

    Other alternatives are befriending the whole swap template function, or befriending the particular specialization of the swap function when applied to the same T that the template has been instantiated with. The first of the options is simple in code, but it grants access to all of the specializations of the swap template, and that might have bad side effects. Befriending the particular swap specialization solves that issue, but is a bit more complex to implement (you need to forward declare the class template, then the swap template, then define the class template, and finally define the swap template).

    More on this in this other answer, where the different options and syntaxes are explained with more detail.

    As to the particular error message of unresolved external, that is due to how identifier lookup works. When you used swap(*this,other); inside a member function, lookup starts inside the class, and tries to find an appropriate swap. It first looks in the class context and finds the declaration of the friend free function, so lookup does not continue going outwards and adds a dependency to that particular free function. It adds the dependency and waits for the linker to locate the appropriate symbol. Because the compiler never considered the templated swap at namespace level, it never actually instantiated it, but even if it had instantiated that template, the dependency inside the operator= member function is on a free function, not that specialization.

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a JSP page retrieving data and when single or double quotes are
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I want to count how many characters a certain string has in PHP, but

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.