My code as follows,and the compiler shows "C2910: 'Gobj<std::string>::operator []' : cannot be explicitly specialized\genericarray.cpp 32",it seems a hopeless implementation ,but I know I always look on the dark side too much,any kinds of help would be appreciated.
string &Gobj<string>::operator [](int i){
return str[i];
};//get rid of this extra ; as well
As for the reason why:
***From C++ Templates the comple guide ***
3.3 Specializations of Class Templates
You can specialize a class template for certain template arguments.
Similar to the overloading of function templates (see page 15),
specializing class templates allows you to optimize implementations for certain types
or to fix a misbehavior of certain types for an instantiation of the class template.
However, if you specialize a class template, you must also specialize all member functions.
Although it is possible to specialize a single member function,
once you have done so, you can no longer specialize the whole class.
To specialize a class template, you have to declare the class
with a leading template<> and a specification of the types for
which the class template is specialized. The types are used as a template
argument and must be specified directly following the name of the class:
1 2 3 4
template<>
class Stack<std::string> {
…
};
For these specializations, any definition of a
member function must be defined as an "ordinary" member function,
with each occurrence of T being replaced by the specialized type: