My code does not compile. Below is my code
template <typename T>
class TemplateClass
{
const T constMember;
public:
TemplateClass()
{
constMember = T();
}
};
int main()
{
TemplateClass <int> obj;
}
I get this error :
error: uninitialized member 'TemplateClass<int>::constMember' with 'const' type 'const int'
I thought that constructor are used to initialize data members. What’s wrong????
You are not initializing the const member, you are just assigning to it.
Initialization of members can only be done using a member initialization list.
For example: