简体   繁体   English

错误C2783无法推断模板参数

[英]error C2783 Could not deduce template arguments

I am stuck with this error . 我被这个错误所困扰。 I have found a workaround too but it kind of kills the whole purpose of exercise. 我也找到了一种解决方法,但它却扼杀了锻炼的全部目的。

I am trying to create a function which will take two iterators pointing to same container . 我试图创建一个函数,它将使用两个指向相同容器的迭代器。 I will find the sum of elements between them. 我会发现它们之间元素的总和。 I created general function for the sequential containers like vector which works fine. 我为向量之类的顺序容器创建了通用功能,效果很好。 I overloaded the same function for associative containers. 我为关联容器重载了相同的功能。 This is the one giving error. 这是一个错误。

map<string,double> myMap;
myMap["B"]=1.0;
myMap["C"]=2.0;
myMap["S"]=3.0;
myMap["G"]=4.0;
myMap["P"]=5.0;

map<string,double>::const_iterator iter1=myMap.begin();
map<string,double>::const_iterator iter2=myMap.end();

cout<<"\nSum of map using the iterator specified range is: "<<Sum(iter1,iter2)<<"\n"; 
//Above line giving error. Intellisense is saying: Sum, Error: no instance of overloaded function "Sum" matches the argument list.

//function to calculate the sum is listed below (It appears in a header file with <map> header included):
template <typename T1,typename T2>
double Sum(const typename std::map<T1,T2>::const_iterator& input_begin,const typename std::map<T1,T2>::const_iterator& input_end)
{
double finalSum=0;
typename std::map<T1,T2>::const_iterator iter=input_begin;

for(iter; iter!=input_end; ++iter)
{
    finalSum=finalSum+ (iter)->second;
}

return finalSum;
}

Compilation error is: 1>c:\\documents and settings\\ABC\\my documents\\visual studio 2010\\projects\\demo.cpp(41): error C2783: 'double Sum(const std::map::const_iterator &,const std::map::const_iterator &)' : could not deduce template argument for 'T1' 编译错误为:1> c:\\ documents and settings \\ ABC \\ mydocuments \\ visual studio 2010 \\ projects \\ demo.cpp(41):error C2783:'double Sum(const std :: map :: const_iterator&,const std :: map :: const_iterator&)':无法推断出“ T1”的模板参数

Workaround: 解决方法:

If call Sum(iter1,iter2) is replaced with Sum < string,double > (iter1,iter2), it compiles fine. 如果将调用Sum(iter1,iter2)替换为Sum <string,double>(iter1,iter2),则编译良好。

Was I trying to do something impossible as per C++ standards in the first place? 我是否首先尝试按照C ++标准做一些不可能的事情?

The error is actually quite clear, in the following template: 在以下模板中,该错误实际上很明显:

template <typename T1,typename T2>
double Sum(const typename std::map<T1,T2>::const_iterator& input_begin,
           const typename std::map<T1,T2>::const_iterator& input_end)

The types T1 and T2 cannot be deduced from the argument at the place of call. T1T2类型不能从调用位置的参数推导出。 This is defined as such in the standard and if you think about it (in the general case) it makes sense. 在标准中对此进行了定义,如果您考虑一下(通常情况下),这是有道理的。

Consider that instead of std::map<>::const_iterator you had sometemplate<T>::nested_type and that the argument at the place of call was an int . 考虑到不是std::map<>::const_iterator而是使用了sometemplate<T>::nested_type并且调用位置的参数是int If the compiler had to deduce the type, it would have to instantiate sometemplate for all possible types T in the universe (infinite set) and find for which of them the nested type nested_type is a typedef to int . 如果编译器必须推断出类型,则必须为sometemplate中的所有可能类型T (无限集)实例化一个sometemplate ,并找出嵌套类型nested_typeint的typedef对于它们中的nested_type一个。

As someone points out in the comments you can change the template so that instead of being templated on the key and value types of the map it just takes the iterators. 正如有人在注释中指出的那样,您可以更改模板,这样就不必使用映射的键和值类型作为模板,而只需使用迭代器即可。


Delegating extraction of the value 委托提取价值

This is a workaround to providing a single implementation of Sum that can deal with both sequential and associative containers. 这是提供单一Sum实现的解决方法,该实现可以处理顺序容器和关联容器。

namespace detail {
template <typename T> T valueOf( T const & t ) { return t; }
template <typename K, typename V>
V valueOf( std::pair<const K, V> const & p ) {
   return p.second;
}
}
template <typename Iterator>
double Sum( Iterator begin, Iterator end ) {
  double result = 0;
  for (; begin != end; ++begin) {
     result += detail::valueOf(*begin);
  }
  return result;
}

I have not tested the code, but that should do it. 我没有测试代码,但是应该这样做。 And this is probably much simpler than using SFINAE on the Sum template. 这可能比在Sum模板上使用SFINAE更为简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误C2783:无法推断出结构参数的模板参数 - error C2783: could not deduce template argument for structure argument C2783:无法推断帮助器 function 的模板参数 - C2783: Could not deduce template argument for a helper function 奇怪的模板错误:错误C2783:无法推断模板参数 - Strange Template error : error C2783: could not deduce template argument 错误C2783:'_Ty && std :: forward(remove_reference <_Ty> :: type &&)throw()':无法推断'_Ty'的模板参数 - error C2783: '_Ty &&std::forward(remove_reference<_Ty>::type &&) throw()' : could not deduce template argument for '_Ty' C ++:C2783错误实例化类模板 - c++: C2783 error instanting class template 表达式模板:错误C2784&#39;无法推断出模板参数&#39; - Expression Templates: error C2784 'could not deduce template argument' 错误C2784:无法推断模板参数 - error C2784: Could not deduce template argument 模板C ++错误:无法推断出模板参数 - Templates C++ error : could not deduce template argument 编译错误 C++:无法推断“T”的模板参数 - Compile error C++: could not deduce template argument for 'T' C ++ | 无法推导T的模板参数| 没有参数的虚函数(display()) - C++ | Could not deduce template argument for T | Void function with no arguments (display() )
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM