What’s wrong with this code?
class School {
public:
template<typename T> size_t count() const;
private:
vector<Boy*> boys;
vector<Girl*> girls;
};
template<> size_t School::count<Boy>() const {
return boys.size();
}
My compile says
error: specialization of ‘size_t School::count() [with T = Boy]’
after instantiation
Could you please help?
ps. This is how I’m going to use it later:
School s;
size_t c = s.count<Boy>();
Have you accidentally called
count<Boy>inSchoolbefore it is declared? One way to reproduce your error isYou need to move the definition of
count_boys()after all template members are specialized.