错误在模板函数中,即使我并没有使用它。

huangapple go评论68阅读模式
英文:

error in template function when i even didn't use it

问题

Here is the translated code part you requested:

#include <iostream>
#include <string>
#include <typeinfo>
using namespace std;

template<class T>
T maxn(T* a, int n);

template<> void maxn(const char* s[], int n);

int main()
{
    const char* words[3] = { "one", "two", "three" };
    return 0;
}

template<> void maxn(const char* s[], int n)
{
    cout << "ok";
}

template<class T>
T maxn(T* a, int n)
{
    T max = a[0];
    for (int i = 1; i < n; i++)
    {
        if (a[i] > max)
        {
            max = a[i];
        }
    }
    return max;
}

I've removed the non-translated code and kept only the translated parts.

英文:
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;typeinfo&gt;
using namespace std;

template&lt;class T&gt;
T maxn(T* a,int n);

template&lt;&gt; void maxn(const char* s[],int n);

int main()
{
//    int n6[6]{21,56,89,49,258,41};
//    double n4[4]{2.1,5.6,8.9,4.9};
//    auto m=maxn(n6,6);
//    auto n=maxn(n4,4);
//    cout&lt;&lt;m&lt;&lt;endl;
//    cout&lt;&lt;n&lt;&lt;endl;
    const char* words[3]={&quot;one&quot;,&quot;two&quot;,&quot;three&quot;};
//    maxn(words,3);
    return 0;
}

template&lt;&gt; void maxn(const char* s[],int n)
{
	cout&lt;&lt;&quot;ok&quot;;
}

template&lt;class T&gt;
T maxn(T* a,int n)
{
	T max=a[0];
	for (int i=1;i&lt;n;i++)
	{
		if (a[i]&gt;max)
		{
			max=a[i];
		}
	}
	return max;
}

I want to write an explicit specialization of maxn() that passes a char*[] array as a parameter, but it reports the following error when I don't even call the function:

> [Error] template-id 'maxn&lt;>' for 'void maxn(const char**, int)' does not match any template declaration
>
> [Note] candidate is: 'template&lt;class T> T maxn(T*, int)'

答案1

得分: 1

The specialization must take a T* and return a T. Your specialization returns void, that is not correct.

Either return T like this:

#include <iostream>
#include <string>

using namespace std;

template<class T>
T maxn(T* a, int n);

template<> const char* maxn(const char* s[], int n);

int main()
{
    const char* words[3] = {"one", "two", "three"};
    return 0;
}

template<> const char* maxn(const char* s[], int n)
{
    cout << "ok";
    return nullptr; // Fix this
}

template<class T>
T maxn(T* a, int n)
{
    T max = a[0];
    for (int i = 1; i < n; i++)
    {
        if (a[i] > max)
        {
            max = a[i];
        }
    }
    return max;
}

Or define the primary template like this:

template<class T, class Ret>
Ret maxn(T* a, int n);
英文:

The specialization must take a T* and return a T. Your specialization returns void, that is not correct.

Either return T like this:

#include &lt;iostream&gt;
#include &lt;string&gt;

using namespace std;

template&lt;class T&gt;
T maxn(T* a,int n);

template&lt;&gt; const char* maxn(const char* s[],int n);

int main()
{
    const char* words[3]={&quot;one&quot;,&quot;two&quot;,&quot;three&quot;};
    return 0;
}

template&lt;&gt; const char* maxn(const char* s[],int n)
{
    cout&lt;&lt;&quot;ok&quot;;
    return nullptr; // Fix this
}

template&lt;class T&gt;
T maxn(T* a,int n)
{
    T max=a[0];
    for (int i=1;i&lt;n;i++)
    {
        if (a[i]&gt;max)
        {
            max=a[i];
        }
    }
    return max;
}

Or define the primary template like this:

template&lt;class T,class Ret&gt;
Ret maxn(T* a,int n);

答案2

得分: 1

Since we can just overload function templates with ordinary non-template function, there is no need to provide an explicit specialization for the function template as shown below.

Looking at your code (the return type and argument type in the explicit specialization in particular), it seems this is what you were looking for.

template<class T>
T maxn(T* a, int n);

// removed template<> from here so that this becomes a non-template function
void maxn(const char* s[], int n)
{
    cout << "ok";
}

template<class T>
T maxn(T* a, int n)
{
    T max = a[0];
    for (int i = 1; i < n; i++)
    {
        if (a[i] > max)
        {
            max = a[i];
        }
    }
    return max;
}
int main()
{

    const char* words[3] = {"one", "two", "three"};
    maxn(words, 3); // works now
}

Working demo

英文:

Since we can just overload function templates with ordinary non-template function, there is no need to provide an explicit specialization for the function template as shown below.

Looking at your code(the return type and argument type in the explicit specialization in particular), it seems this is what you were looking for.

template&lt;class T&gt;
T maxn(T* a,int n);

//removed template&lt;&gt; from here so that this becomes a non-template function
void maxn(const char* s[],int n)
{
    cout&lt;&lt;&quot;ok&quot;;
}
 
template&lt;class T&gt;
T maxn(T* a,int n)
{
    T max=a[0];
    for (int i=1;i&lt;n;i++)
    {
        if (a[i]&gt;max)
        {
            max=a[i];
        }
    }
    return max;
}
int main()
{

    const char* words[3]={&quot;one&quot;,&quot;two&quot;,&quot;three&quot;};
    maxn(words,3);//works now
}

Working demo

huangapple
  • 本文由 发表于 2023年5月20日 21:11:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76295396.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定