【发布时间】:2014-11-29 20:26:37
【问题描述】:
我有一个带有方法的模板类template <typename T> class MyClass:
void add (T& item) {
data[indexToInsert++] = &item; // data is an array of T*
}
主要:
MyClass<int> thing;
thing.add(10);
在第二行,我得到这个错误:
no matching function for call to MyClass::add(int)
为什么会这样?
【问题讨论】:
-
像
T&这样的非 const 左值引用不能绑定到像10这样的右值。无论如何,编译器应该如何获取10的地址?
标签: c++ class templates methods reference