c++ - 无法将 ‘std::string {aka std::basic_string<char>}’ 转换为 ‘char’ 作为返回

标签 c++ string append

我在函数外部返回字符串时遇到问题。之前是否应该进行某种转换?

我在类外使用 public const int val_int[ ]const char* val_rom[ ]

以及类内部:

private:
    char* roman;

public:
    char arab2rzym(int arabic) throw (RzymArabException){
        if( arabic < 0){
            throw RzymArabException(arabic + " is too small");
        }
        else if(arabic > 3999){
            throw new RzymArabException(arabic + " is too big");
        }
        std::string roman;

        for(int i=12; i>=0; i--){
            while(arabic>=val_int[i]){
                roman.append(val_int[i]);
                arabic-=val_int[i];
            }
        }
        return roman;

    }

最佳答案

您是否可以将该方法的签名更改为:

std::string arab2rzym(int arabic)

如果是这样,您可以在实际需要的地方使用该字符串。

无论如何,我建议您查看std::string reference ,特别是operator[]和方法c_str()

关于c++ - 无法将 ‘std::string {aka std::basic_string<char>}’ 转换为 ‘char’ 作为返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15734238/

相关文章:

c++ - 我应该如何正确地将 packaged_task move 到 lambda?

java - 使用指数解析字符串 (Java)

python - 将列表 append 到列表以创建列表列表会在想要使用列表列表的索引 : python 更改值时出现问题

jQuery 多次追加克隆

C++ 程序崩溃,退出代码为 : 9 (SIGKILL)

c++ - 为什么可以用多态来代替switch或else-if语句?

c++ - "error: subscripted value is not an array, pointer, or vector"我正在使用一个字符串

javascript - append 动态创建的表 javascript

c++ - Windows 剪贴板不会保留 ASCII 字符

c# - Flex/Flash/Java/C# 中的漂亮日期文本