error: explicit specialization of undeclared template class

Alex Vergara

I have this interface:

template <class T>
    class Builder {
        public:
            // Virtual destructor
            virtual ~Builder(){}
            // Builds a concrete instance of the implementor
            virtual T build() const =0;
    };

and the following concrete implementation:

template <class T>
    class ConcreteBuilder<T> : Builder<T> {
        public:
            // Virtual destructor
            virtual ~ConcreteBuilder(){}
            // override the build() base method from Builder<T>
            virtual T build() override {
                return 0;
            }
    };

Suppose the next mock:

class MockedClass {
    public:
        std::string return_hi() const {
            return std::string {"Hi!"};
        }

        int return_num() const {
            return 10;
        }
};

And the following:

MockedClass mc;
std::cout << mc.return_hi() << std::endl;
std::cout << mc.return_num() << std::endl;

ConcreteBuilder<MockedClass>;

And the error:

error: explicit specialization of undeclared template class 'ConcreteBuilder'

If I remove the class parameter from the declaration of ConcreteBuilder: class ConcreteBuilder : Builder<T> everything works, but I have the doubt about what I am allowed to do this:

ConcreteBuilder; (no type parameters)

1 - Why this error happens? 2 - How can I constraint my ConcreteBuilder in order to make mandatory to specify the type parameter for ConcreteBuilder?

Thanks.

Anoop Rana

The correct syntax to inherit from Builder<T> while defining the ConcreteBuilder template would be as shown below:

template <class T>
//-------------------v------------------->removed <T> from here
class ConcreteBuilder : Builder<T> {
    public:
        // Virtual destructor
        virtual ~ConcreteBuilder(){}
        // override the build() base method from Builder<T>
        virtual T build() override {
            return 0;
        }
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

explicit specialization in template class error

Can you forward declare an explicit specialization of undeclared template class?

explicit specialization of a function template (which is a member of a class template) produces "partial specialization is not allowed" error, why?

Explicit template specialization for templated constructor of templated class

Template explicit specialization in class using inheritance

Explicit template specialization cannot have a storage class - member method specialization

explicit specialization "..." is not a specialization of a function template

Explicit specialization of a function template for a fully specialized class template

explicit template specialization with no explicit specialization declaration

Explicit specialization of template variable

Where should the definition of an explicit specialization of a class template be placed in C++?

VS2019 explicit specialization requires template<> error with 'using' keyword

template<> for the explicit specialization of a member enumeration

Explicit instantiation of function template specialization

C++ template specialization inside a class error

Clang partial class template specialization error

class template specialization with template

A storage class is not allowed in an explicit specialization

Undeclared template class error when using friend class with templates

Error: class template partial specialization contains a template parameter that cannot be deduced

Why does explicit method specialization of a template class work without its prototype declaration inside the class

Is it legal to define a hidden friend in an explicit specialization of an otherwise non-defined member class of a class template?

Template class specialization with template class

Template : class specialization

Check if class is a template specialization?

Nested class template specialization

Class template member specialization

Class template method specialization

namespace specialization in template class