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

  • Home
  • SEARCH
  • 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 7172023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:31:52+00:00 2026-05-28T15:31:52+00:00

I have to implement template specialization, in implementing the constructor for specialized template class

  • 0

I have to implement template specialization, in implementing the constructor for specialized template class compiler generates some errors. Following is my code:

#include <iostream>
using namespace std;

// class template

template <typename T>
class mycontainer
{

T element;
  public:
    mycontainer (T arg);
    T increase () {return ++element;}
};

// class template specialization
template <>
class mycontainer <void> {
  int element;
  public:
    mycontainer (int arg);

    char uppercase ()
    {
    return element;
    }
};

template<typename T> mycontainer<T>::mycontainer(T arg){
    cout << "hello T" << endl;
}

template<typename T> mycontainer<void>::mycontainer(int arg){
    cout << "hello Empty" << endl;
}

int main () {
    mycontainer<int> myint (7);
    mycontainer<void> myvoid (6);
    cout << myint.increase() << endl;
    return 0;
}

The code generate these errors:

test.cpp:31:22: error: prototype for ‘mycontainer<void>::mycontainer(int)’ does not match any in class ‘mycontainer<void>’
test.cpp:16:26: error: candidates are: mycontainer<void>::mycontainer(const mycontainer<void>&)
test.cpp:19:5: error:                 mycontainer<void>::mycontainer(int)

Any clue on how to resolve these errors ?

  • 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-28T15:31:53+00:00Added an answer on May 28, 2026 at 3:31 pm

    Your syntax for full specialization is wrong, you SHOULDN’T use template<typename T> mycontainer<void> or not even template<>

    Why? See quote from C++ template book:

    A full specialization declaration is identical to a normal class declaration in this way (it is not a template declaration). The only differences are the syntax and the fact that the declaration must match a previous template declaration. Because it is not a template declaration, the members of a full class template specialization can be defined using the ordinary out-of-class member definition syntax (in other words, the template<> prefix cannot be specified):

    So can either do

    mycontainer<void>::mycontainer(int arg){
        cout << "hello Empty" << endl;
    }
    

    or do:

    #include <iostream>
    using namespace std;
    
    // class template
    
    template <typename T>
    class mycontainer
    {
    
    T element;
      public:
        mycontainer<T>::mycontainer(T arg)
        {
            cout << "hello T" << endl;
        }
        T increase () {return ++element;}
    };
    
    // class template specialization
    template <>
    class mycontainer <void> {
      int element;
    public:
        mycontainer (int arg)
        {
            cout << "hello Empty" << endl;
        }
    
        char uppercase ()
        {
        return element;
        }
    };
    
    
    int main () {
        mycontainer<int> myint (7);
        mycontainer<void> myvoid (6);
        cout << myint.increase() << endl;
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following class template: template<class T, unsigned N> class MyClass; where T
I have a class similar to the following: class SomeClass { public: template<typename... Args>
I have a class template Foo<T> . I'd like to implement a non-member function
I have the following code snippet of a small Thread class I am trying
I have found that I often have to implement some sort of a scheduler
Let's say I have to implement a piece of T-SQL code that must return
Consider the following example code: class Foo { }; class Bar : public Foo
I have to implement an Form View, or in other words: A class that
I have a template class in C++ (somewhat simplified): template<typename T> struct C {
I have been trying to implement my own linked list class for didactic purposes.

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.