尝试将模板实现到 readInSearchCriterion 方法中以处理 double /字符串/日期。无法推断“T”的模板参数是当前状态。
在 UserInterface.cpp 中
声明:
template <typename T> T readInSearchCriterion() const;
定义:
template<typename T>
T UserInterface::readInSearchCriterion() const
{
T val;
cout << "Enter search value: ";
cin >> val;
return val;
}
在CashPoint.cpp中,double(amount)存储搜索条件(eg.50)
amount = theUI_.readInSearchCriterion();
最佳答案
当你写一个模板函数时:
template<class T> void foo( T arg );
你假设调用它指定类型:
foo<int>( 1 );
如果编译器可以计算或推断类型,则可以省略完整形式并使用缩写形式:
foo( 1 ); // same as before, <int> deduced from arg type, which is int(1) in this case
编译器不允许根据语言规则从返回类型中推断出来(为什么这样超出了这个问题的范围)所以如果你只在返回中使用模板类型,请使用完整形式:
template<class T> T foo();
int i = foo<int>(); // ok
int i = foo(); // error
这与链接器未解析的符号无关,这很可能意味着您将实现放在 cpp 文件中,但它必须在您使用它的任何地方可见(或者您需要对它应该使用的所有类型使用显式实例化)。最简单的解决方案 - 将实现放入标题中。
关于c++ - 模板有问题。错误 : Cannot deduce template argument for type 'T' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23547722/