参考書の演習問題に取り組んでおり、ある問題のプログラムが適切に作成ができずコンパイルエラーが発生し困っています。
問題は以下のとおりです。
配列の全要素の最小値を求める関数テンプレートを作成せよ。
なお、最も小さい文字列を求められるようにするために、const char* 型に明示的に特殊化したものをあわせて作成すること。 (新版明解 C++ 入門編より引用)
OS: lubuntu コンパイラ: g++
作成したソースコードを以下に示します。
#include<iostream>
#include<cstring>
#define M 16
using namespace std;
template <class Type> Type minof(const Type x[], int n)
{
Type min = x[0];
for(int i = 1; i < n; i++)
if(x[i] < min)
min = x[i];
return min;
}
template <> const char* minof<const char*>(const char p[][M], int n)
{
int min = 0;
for(int i = 1; i < n; i++)
if(strcmp(p[i], p[min]) < 0)
min = i;
return p[min];
}
int main()
{
const int n1 = 4;
int n[n1];
double d[n1];
char s[n1][M];
for(int i = 0; i < n1; i++){
cout << "整数[" << i << "] : "; cin >> n[i];
}
for(int i = 0; i < n1; i++){
cout << "実数[" << i << "] : "; cin >> d[i];
}
for(int i = 0; i < n1; i++){
cout << "文字列[" << i << "] : "; cin >> s[i];
}
cout << "最小の整数 : " << minof(n,n1) << "\n";
cout << "最小の実数 : " << minof(d,n1) << "\n";
cout << "最小の文字列: " << minof<const char*>(s,n1) << "\n";
}
en0905.cpp:18:25: error: template-id ‘minof<const char*>’ for ‘const char* minof(const char (*)[16], int)’ does not match any template declaration
template <> const char* minof<const char*>(const char p[][M], int n)
^
en0905.cpp: In function ‘int main()’:
en0905.cpp:50:60: error: no matching function for call to ‘minof(char [4][16], const int&)’
cout << "最小の文字列: " << minof<const char*>(s,n1) << "\n";
^
en0905.cpp:50:60: note: candidate is:
en0905.cpp:7:28: note: template<class Type> Type minof(const Type*, int)
template <class Type> Type minof(const Type x[], int n)
^
en0905.cpp:7:28: note: template argument deduction/substitution failed:
en0905.cpp:50:60: note: cannot convert ‘s’ (type ‘char [4][16]’) to type ‘const char* const*’
cout << "最小の文字列: " << minof<const char*>(s,n1) << "\n";
^