I was trying to make a Container class that will be using an object of a class Comparator which will be passed through the template.
So I am doing something like:
template<typename T>
class Container{
const T c;
};
struct Comparator{
public:
bool operator()(const int a, const int b)const{
return a < b;
}
};
int main(){
Container<Comparator> container;
return 0;
}
When I am trying to compile this code I am getting the following error:
e.cpp:28:27: error: uninitialized const member in 'class Container<Comparator>'
28 | Container<Comparator> container;
| ^~~~~~~~~
e.cpp:16:13: note: 'const Comparator Container<Comparator>::c' should be initialized
16 | const T c;
|
To get rid of this error what I did is just added a default constructor in the Comparator class:
struct Comparator{
public:
Comparator(){
}
bool operator()(const int a, const int b)const{
return a < b;
}
};
But the weird thing I observed is that when I am making a const object of the class Comparator as a normal variable (not the data member of any class) I am not getting any kind of error.
struct Comparator{
public:
bool operator()(const int a, const int b)const{
return a < b;
}
};
int main(){
const Comparator comp;
return 0;
}
So my question is why I am getting errors when making a const object of the Comparator type within a class and not when making a normal const object of the same type?