Home > Enterprise >  error: explicit specialization of undeclared template class
error: explicit specialization of undeclared template class

Time:06-08

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.

CodePudding user response:

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