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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:06:59+00:00 2026-05-16T15:06:59+00:00

The following code compiles properly. #include <string> template <typename T, typename U> class Container

  • 0

The following code compiles properly.

#include <string>

template <typename T, typename U>
class Container
{
private:
    T value1;
    U value2;
public:
    Container(){}
    void doSomething(T val1, U val2);
};

template<typename T, typename U>
void Container<typename T, typename U>::doSomething(T val1, U val2)
{
    ; // Some implementation
}

template <>
class Container<char, std::string>
{
private:
    char value1;
    std::string value2;
public:
    Container(){}
    void doSomething(char val1, std::string val2)
    {
        ; // Some other implementation
    }
};

But if I try to define void doSomething(char val1, std::string val2) outside, I get the following error.

#include <string>

template <typename T, typename U>
class Container
{
private:
    T value1;
    U value2;
public:
    Container(){}
    void doSomething(T val1, U val2);
};

template<typename T, typename U>
void Container<typename T, typename U>::doSomething(T val1, U val2)
{
    ; // Some implementation
}

template <>
class Container<char, std::string>
{
private:
    char value1;
    std::string value2;
public:
    Container(){}
    void doSomething(char val1, std::string val2);
};

template<>
void Container<char,std::string>::doSomething(char val1, std::string val2)
{
    ; // Some other implementation
}

Error:

Error 1 error C2910:
‘Container::doSomething’
: cannot be explicitly
specialized c:\users\bharani\documents\visual
studio
2005\projects\templates\template
specialization\templatespecializationtest.cpp 35

What mistake do I commit?

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-05-16T15:07:00+00:00Added an answer on May 16, 2026 at 3:07 pm

    You are not explicitly specializing a member function. But you are defining the member function of an explicit (class template-) specialization. That’s different, and you need to define it like

    inline void Container<char,std::string>::doSomething(char val1, std::string val2)
    {
        ; // Some other implementation
    }
    

    Note that the “inline” is important, because this is not a template and it is not implicitly inline if it is defined outside the class. It’s needed to be inline to avoid duplicate linker symbols if you include the header into more than one translation unit.

    Would you have a template in your explicit specialization, your syntax would have to be used:

    template <>
    class Container<char, std::string>
    {
    private:
        char value1;
        std::string value2;
    public:
        Container(){}
    
        template<typename T, typename U>
        void doSomething(T val1, U val2) { /* primary definition */ }
    };
    
    template<>
    inline void Container<char,std::string>::doSomething(char val1, std::string val2)
    {
        ; // Some other implementation
    }
    

    You also have an error in your first code. You need to define the out of class definition like this, without “typename” in the argument list of the class template

    template<typename T, typename U>
    void Container<T, U>::doSomething(T val1, U val2) 
    {
        ; // Some implementation
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that compiles and works well: template<typename T> T GetGlobal(const
I have the following code: #include <stdio.h> template<int A> class Thing { // 5
Is the following code safe? (I already know it compiles properly.) void Tile::clear() {
The following source code compiles correctly with Visual Studio 2010: namespace NS { class
The following code (which compiles and executes properly, doing what I want) is a
I have the following code package myPackage; import org.neo4j.graphdb; import org.neo4j.kernel.EmbeddedGraphDatabase; public class dbServlet
The following code compiles in Visual C++ and gcc, but fails with Code Warrior
the following code compiles with Visual Studio 2008 but not with g++ on Mac
I came across the following code that compiles fine (using Visual Studio 2005): SomeObject
How is it possible that following code even compiles? As far as I can

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.