user1164934
user1164934

Reputation:

template specialization implementation

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 ?

Upvotes: 2

Views: 2801

Answers (3)

RoundPi
RoundPi

Reputation: 5947

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;
}

Upvotes: 2

Mike Seymour
Mike Seymour

Reputation: 254751

mycontainer<void> is not a template, and neither is its constructor, so the constructor definition should just be:

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

Upvotes: 2

pmr
pmr

Reputation: 59841

Your prototype

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

does not match the one in the specialization. Leave the template parameter empty.

That being said: Your C++ does not look like you are ready for using templates. You should get the basics right first.

Upvotes: -1

Related Questions