【发布时间】:2015-07-01 09:13:33
【问题描述】:
为什么这段代码有错误:
template <typename T>
class CLs{
public:
void print(T* p){ p->print(); }
};
void main() {
CLs<int> c1; // compilation OK
CLs<double> c2; // compilation OK
double d=3;
c2.print(&d);
}
我的讲师说c2.print(&d); 行有错误:
Compilation Error: Member function is instantiated only if called.
他是什么意思?
【问题讨论】:
-
变量
d是double类型,所以当你在函数体中调用c2.print(&d)就像调用d.print()但 d 是一个具体类型并且没有成员函数print.
标签: c++ templates member-functions