我在標題中出現錯誤。它指向了for循環聲明的那一行。關於發生什麼的任何想法?從'char *'無效轉換爲'char'
#include <iostream>
template <typename T>
T max(T* arr, size_t n)
{
if (!n)
throw("Can't take the max of an empty array, bro.");
T top = arr[0];
for (T* i(arr+1), j(arr+n); i != j; ++i)
if (*i > top)
top = *i;
return top;
}
int main()
{
char S[] = "kjadkjhdjasjkdaskjdsahd";
std::cout << max(S, strlen(S));
return 0;
}
FWIW,這可以用'std :: max_element'替換(並且用'std :: string'更好),並且缺少'strlen'的頭文件。 – chris
這沒什麼好玩的,雖然 – user3461018