从'char *'到'char'的无效转换 [英] invalid conversion from 'char*' to 'char'

查看:88
本文介绍了从'char *'到'char'的无效转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题出现错误.它指向声明for循环的行.有什么想法吗?

I'm getting the error in the title. It's pointing to the line where the for-loop is declared. Any ideas on what is happening?

#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;
} 

推荐答案

在此行:

for (T* i(arr+1), j(arr+n); i != j; ++i)

仅将i声明为指向T的指针,将j声明为T的实例.正确的声明为:

Only the i is declared as pointer to T, j is declared as an instance of T. The correct declaration is:

for (T* i(arr+1),* j(arr+n); i != j; ++i)

顺便说一句,您应该为strlen添加include:

And by the way, you should add include for strlen:

#include <cstring>

这篇关于从'char *'到'char'的无效转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆