1

Can someone please explain me why is this not compiling...the specialized function of a specialized class ??? In the specialized version of the templated class the specialized function is not compiling.

        #include<iostream>
        using namespace std;

    //Default template class
        template<typename T>
        class X
        {
        public:
            void func(T t) const;
        };


        template<typename T>
        void X<T>::func(T b) const
        {
            cout << endl << "Default Version" << endl;
        }


//Specialized version        
        template<>
        class X<int>
        {
            public:
            void func(int y) const;
        };

        template<>
        void X<int>::func(int y)
        {
            cout << endl << "Int Version" << endl;
        }

        int main()
        {
            return 0;
        }
4

2 回答 2

3

类模板的显式特化是具体类,而不是模板,因此您可以(或者更确切地说,应该)只写:

// template<>
// ^^^^^^^^^^
// You should not write this

void X<int>::func(int y) const
//                       ^^^^^
//                       And do not forget this!
{
    cout << endl << "Int Version" << endl;
}

从而省略了template<>部分。

另外,请注意您的func()函数const在声明中是 - 限定的 - 所以const即使在定义中也必须使用限定符。

这是一个活生生的例子

于 2013-06-09T13:13:52.557 回答
1

我认为你离开了尾随的 const 修饰符

于 2013-06-09T13:12:53.040 回答