简体   繁体   English

重载的模板功能帮助-C ++

[英]Overloaded Template Function Help - C++

I keep getting an error "no instance of overloaded function "printArray" matches the argument list. Will someone please tell me why? I'm trying to overload a template function so that it displays elements of an array starting and ending at specified positions. 我不断收到错误消息“没有重载函数“ printArray”的实例与参数列表匹配。有人会告诉我为什么吗?”我试图重载模板函数,以便它显示在指定位置开始和结束的数组元素。

I have my initial template and function: 我有初始模板和功能:

template< typename T >
void printArray( const T *array, int count )

And the function that I'm trying to overload. 还有我要重载的功能。

template< typename T >
void printArray(int lowSubscript, int highSubscript)

and my call: 和我的电话:

// display elements 1-3 of array a
   cout << "Array a from positions 1 to 3 is:\n";
   elements = printArray(1,3);

my call for the first printArray shows no errors: 我对第一个printArray的调用未显示任何错误:

// display array a using original printArray function
   cout << "\nUsing original printArray function\n";
   printArray( a, ACOUNT );

Your second overload of printArray is a template, but the signature is not dependent on the template parameter T . printArray第二个重载是模板,但签名不依赖于模板参数T Therefore you must specify it when calling, eg printArray<int>(1,3) . 因此,在调用时必须指定它,例如printArray<int>(1,3)

However, it is unclear which array is being printed: did you forget to add a parameter for the array, eg printArray(a,1,3) ? 但是,不清楚要打印哪个数组:您是否忘记为数组添加参数,例如printArray(a,1,3) In which case, you might be able to have T deduced from that parameter, as it is in the printArray(a,count) version. 在这种情况下,您可以从该参数推导出T ,就像在printArray(a,count)版本中那样。

Also, the return type of void means you cannot write elements=printArray(1,3) even if the template parameter could be deduced. 另外, void的返回类型意味着即使可以推导出template参数,也不能编写elements=printArray(1,3)

Your second printArray does not use T, and thus should not be a template. 您的第二个printArray不使用T,因此不应作为模板。

The reason the compiler does not recognize it as a candidate is because it can not deduce T. 编译器无法将其识别为候选者的原因是因为它无法推导T。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM