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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:02:09+00:00 2026-05-26T06:02:09+00:00

I want to specialize specific function in template class. Eg: template<class T> class A

  • 0

I want to specialize specific function in template class.

Eg:

template<class T>
class A   
{    
public :  
  void fun1(T val);  
  void fun2(T val1, T val2);
};

template <class T>
void A<T>::fun1(T val)
{
  // some task 1;
}


template <class T>
void A<T>::fun2(T val1, T val2)
{
  // some task 2;
}


template <>
void A<char*>::fun2(char* val1, char* val2)
{
  // some task 2 specific to char*;
}

when I do something like this, I get error saying multiple definition for fun2()
Please let me why this wrong and also the correct way to implement this.

  • 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-26T06:02:09+00:00Added an answer on May 26, 2026 at 6:02 am

    Your method fun2() is not a template method as itself, though it’s a member of a template class. I don’t find the proper technical term but in simple words, specializing fun2() will create an effect of a normal function definition. Putting the definition in header file will give you multiple definition error.

    To solve this problem, just put an inline keyword and the linker error will go away!

    template <> inline // <----- 'inline' will prompt to generate only 1 copy
    void A<char*>::fun2(char* val1, char* val2)
    {
      // some task 2 specific to char*;
    }
    

    Edit: This solves the linker error. But still you cannot use the A<char*>::fun2. Ultimately it boils down to the very fact that you need to specialize the whole class A<char*> or overload the fun2(char*, char*) within A<T>

    template<class T>
    class A
    {
      // constructors
    public:
      //...
      void fun2(char* val1, char* val2)
      {
        //specific case when T = char*
      }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want a specialize template in a pointer-to-member-function case. Is there a way to
I want to serialize this class: public class CarDisplay { public string Name {
Say I have a templated class: template <typename T> class foo { void do_someting(T
Hallo! I want to specialize std::max for a platform-specific fraction type. The prototype for
class FooInterface { virtual void toggle() = 0; }; template <class TYPE, class CONFIG>
I want to make this specialized w/o changing main. Is it possible to specialize
I want to serialize a class to xml and store that in a field
Suppose I have an application with the following code: public class Node { public
I have created custom result class to serialize json data to xml. I want
I want to generalize this: <html> <script src=http://code.jquery.com/jquery-latest.js></script> <script> $(function(){ $('#target1').click(function() { $.post(process_form.php, $(#JqPostForm1).serialize(),

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.