我有一个带有参数化构造函数的模板类。
这是一个最小的例子。以下代码工作正常:
template <typename T>
class my_template
{
public:
my_template () {}
my_template (T Value) : value(Value) {}
T get_value () { return value; }
private:
int value;
};
int main()
{
my_template<int> int_thing (5);
my_template<char> char_thing ('a');
int int_test = int_thing.get_value ();
char char_test = char_thing.get_value ();
}
如果我尝试使用默认构造函数,则不起作用。
更改此行:
my_template<int> int_thing (5);
对此:
my_template<int> int_thing ();
引发此错误:
Severity Code Description Project File Line Suppression State
Error (active) E0153 expression must have class type template_class c:\Nightmare Games\Examples\CPP\template_class\template_class.cpp 39
在这条线上:
int int_test = int_thing.get_value();
我没有最起雾的。从类中删除参数化构造函数对另一个构造函数引发的错误没有影响。C++ 只是讨厌那个默认构造函数。
从理论上讲,我可以在参数中放入一些虚拟数据并稍后更改它,所以它不会阻止我。
但我只需要知道。