加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

具有可变返回类型的C函数,使用’auto’

发布时间:2020-12-16 10:35:21 所属栏目:百科 来源:网络整理
导读:我正在尝试编写一个函数,它返回各种类型,具体取决于if语句. auto parseParameterValue(QString aParameterValueString,int aParameterType){ if(aParameterType == 0) { int result = aParameterValueString.toInt(); return result; } else if(aParameterTy
我正在尝试编写一个函数,它返回各种类型,具体取决于if语句.

auto parseParameterValue(QString aParameterValueString,int aParameterType)
{
    if(aParameterType == 0)
    {
        int result = aParameterValueString.toInt();
        return result;
    }
    else if(aParameterType == 1)
    {
        double result = aParameterValueString.toDouble();
        return result; // <------- compilation error
    }
    else
    {
        return aParameterValueString;
    }
}

不幸的是,我得到的是:

  1. Warning: ‘parseParameterValue’ function uses ‘auto’ type specifier without trailing return type
  2. Error at second return: inconsistent deduction for ‘auto’: ‘int’ and then ‘double’

有没有办法让它发挥作用?

提前致谢.

解决方法

不,函数只能有一种返回类型.

请注意,函数返回类型的处理必须在编译时进行,但是您的函数使用的值在运行时才能知道.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读