You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++传递char数组到模板方法实现enum转字符串报错如何修复?

报错原因

函数getEnumTypeName的第二个参数类型定义错误:

  • 你声明的参数类型const char (*typeValues)[10]是指向长度为10的char类型数组的指针
  • 实际传入的MyClass::typeValue是const char* [10]类型,即元素为const char*、长度为10的指针数组
    二者类型不匹配,因此编译器抛出转换失败的错误。
修复代码

基础修复版

直接修改参数类型即可正常运行:

#include <iostream>
#include <string>
#include <algorithm>

#define stringify(name) #name

struct MyClass
{
    enum class TYPE
    {
        UNKNOWN = 0,
        CLIENT,
        SERVER
    };
    
    MyClass(TYPE t)
        :_type(t)
    {       
    }
    
    TYPE type() const // 建议加const修饰常量成员函数
    {
        return _type;
    }

    inline static const char* typeValue[10] =
    {
        stringify(TYPE::UNKNOWN),
        stringify(TYPE::CLIENT),
        stringify(TYPE::SERVER)
    };
    
private:
    TYPE _type;
};


struct EnumStringify
{
    /**
    * Get enum type string by value
    */
    template <class EnumType>
    // 修改第二个参数类型为const char*数组
    static std::string getEnumTypeName(const EnumType& t, const char* typeValues[])
    {
        int idx = static_cast<int>(t);
        // 建议加边界校验避免越界
        if (idx < 0 || typeValues[idx] == nullptr) return "";
        std::string s(typeValues[idx]);
        return (s.size()) ? s.substr(s.find_last_of(":") + 1) : "";
    }
};

int main()
{
  MyClass a(MyClass::TYPE::CLIENT);
  std::cout << EnumStringify::getEnumTypeName<MyClass::TYPE>(a.type(), MyClass::typeValue) << std::endl;
  // 输出结果:CLIENT
}

typedef优化版

使用typedef(或C++11更推荐的using)优化类型定义确实更好,能统一类型约束,避免写错复杂的数组类型,后续维护也更方便:

#include <iostream>
#include <string>
#include <algorithm>

#define stringify(name) #name

struct MyClass
{
    enum class TYPE
    {
        UNKNOWN = 0,
        CLIENT,
        SERVER,
        TYPE_MAX // 新增枚举最大值,方便定义数组长度
    };
    
    // 用typedef定义枚举字符串数组类型
    typedef const char* TypeStringArray[static_cast<size_t>(TYPE::TYPE_MAX)];
    // C++11及以上也可以用using写法:
    // using TypeStringArray = const char*[static_cast<size_t>(TYPE::TYPE_MAX)];

    MyClass(TYPE t)
        :_type(t)
    {       
    }
    
    TYPE type() const
    {
        return _type;
    }

    inline static TypeStringArray typeValue =
    {
        stringify(TYPE::UNKNOWN),
        stringify(TYPE::CLIENT),
        stringify(TYPE::SERVER)
    };
    
private:
    TYPE _type;
};


struct EnumStringify
{
    template <class EnumType>
    // 直接用类型别名做参数,代码可读性大幅提升
    static std::string getEnumTypeName(const EnumType& t, const MyClass::TypeStringArray typeValues)
    {
        int idx = static_cast<int>(t);
        if (idx < 0 || idx >= static_cast<int>(MyClass::TYPE::TYPE_MAX)) return "";
        std::string s(typeValues[idx]);
        return s.empty() ? "" : s.substr(s.find_last_of(":") + 1);
    }
};

int main()
{
  MyClass a(MyClass::TYPE::CLIENT);
  std::cout << EnumStringify::getEnumTypeName(a.type(), MyClass::typeValue) << std::endl;
}
额外优化建议
  • 新增枚举最大值TYPE_MAX来定义数组长度,避免手动写死大小,新增枚举成员时自动扩容
  • 增加枚举值的边界校验,避免越界访问数组
  • 常量成员函数加const修饰,符合C++编码规范

内容的提问来源于stack exchange,提问作者Pasha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 05:54:02