C++ C++;函数模板问题(未找到实例) 模板 T马特马提卡(T polje[],国际北){ //代码。。。 }
这就是错误所在(没有函数模板“matematika”的实例与指定类型匹配)C++ C++;函数模板问题(未找到实例) 模板 T马特马提卡(T polje[],国际北){ //代码。。。 },c++,c++11,templates,C++,C++11,Templates,这就是错误所在(没有函数模板“matematika”的实例与指定类型匹配) 模板 字符串matematika(字符串polje[],int N){//错误 //代码。。。 } 专业化模板的常规语法要求您在专业化声明中的名称后面提供专业化的模板参数。例如: template<> string matematika(string polje[], int N) { // ERROR //CODE... } 由于使用了int p作为第二个模板参数,因此仍需保留该参数: tem
模板
字符串matematika(字符串polje[],int N){//错误
//代码。。。
}
专业化模板的常规语法要求您在专业化声明中的名称后面提供专业化的模板参数。例如:
template<>
string matematika(string polje[], int N) { // ERROR
//CODE...
}
由于使用了
int p
作为第二个模板参数,因此仍需保留该参数:
template<>
string matematika<string, /*Whatever value of P you want to specialise for*/>(string polje[], int N) {
//CODE...
}
我怀疑您希望在编译时传递一个已知大小的数组,而不是为字符串重载函数
templattemplate<typename T, int P>
T matematika(T (&polje)[P])
#包括
使用名称空间std;
模板
T马特马提卡(T&polje)[N])
{
你想实现什么?你想做模板专门化还是其他什么?你的intp
看起来像是个错误,这个错误导致了你的困惑。这个string matematika(string*,int);//(2)
不是专门化。这意味着有人可以做字符串matematike
,OP会感到非常惊讶。@Yakk我认为很明显会调用(1)和(2)。基本模板规则,这并不奇怪。(2)与(1)无关,它只是具有与(1)相同的函数名当然,我只是注意到意外事件非常重要。
template <class T>
void foo(T t) {}
// One valid way to specialise:
template <>
void foo<int>(int i) {}
// Another valid way to specialise:
template <>
void foo(int i) {}
template<>
string matematika<string, /*Whatever value of P you want to specialise for*/>(string polje[], int N) {
//CODE...
}
template<typename T, int P>
T matematika(T polje[], int N) { // (1)
...
}
template<int P> // T = string
string matematika(string polje[], int N) { // (2)
...
}
int matematika<int, 111>(int*, int); // (1)
string matematika<22>(string*, int); // (2)
template<> // T = string, int P = 10
string matematika<string, 10>(string polje[], int N) { // (3)
...
}
string matematika<string, 10>(string*, int); // (3)
string matematika<string, 11>(string*, int); // (1)
templattemplate<typename T, int P>
T matematika(T (&polje)[P])
#include <iostream>
using namespace std;
template<typename T, int N>
T matematika(T (&polje)[N])
{
cout << "array version: " << polje << endl;
return polje[0];
}
std::string::value_type matematika(std::string const& polje)
{
cout << "string version: " << polje << endl;
return polje[0];
}
int main() {
// your code goes here
cout << "got: " << matematika("test") << endl;
cout << "got: " << matematika(string("string")) << endl;
return 0;
}