这是一个与此问题中的代码无关的问题,关于以下模板函数。
template <class T>
class Object : public Container {
public:
T& object;
Object(const T& obj) : object(obj) {}
};
这是调用构造函数的代码:
template <class T>
void Array::add_element(const T& element)
{
vec.push_back(new Object<T>(element));
}
这段代码编译得很好,但是只要我在其中添加一行main调用它:
Array array;
int i = 3;
array.add_element(i);
我收到编译器警告:error: invalid initialization of reference of type 'int&' from expression of type 'const int'.
那是关于什么的?我通过了int。它不应该自动变成我的const int&吗?为什么编译器会抱怨?