KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
This is an unrelated question about the code in this question , regarding the following template function. template <class T> class Object : public Container { public: T& object; Object(const T& obj) : object(obj) {} }; This is the code that calls the constructor: template <class T> void Array::add_element(const T& element) { vec.push_back(new Object<T>(element)); } This code compiles fine, but as soon as I add a line in main that calls it: Array array; int i = 3; array.add_element(i); I get a compiler warning: error: invalid initialization of reference of type 'int&' from expression of type 'const int' . What is that about? I passed an int in. Shouldn't it automatically be turned into a const int& for me? Why is the compiler complaining?
Tags (comma-separated)
Save Edits
Cancel