从C++中的函数返回一个字符串

Chi*_*ils -1 c++ string

我有一些代码是从姓氏的字符串中生成引用代码.应该采取的第一个char是否元音或没有,然后搜索每个剩余char阵列丢弃元音辅音存储仅在温度在string,则返回临时stringstring refCode;的代码是给我在C和I它转换为C++.代码编译正确地分配第一个值,但是如果if返回true它将在尝试将第二个值分配给temp时失败string.代码超过5个外部.cpps和4 .h秒,所以我将从最小量开始,并将根据需要发布更多.

Protytpe:

string makeRefCode(string lastname, int cNo);
Run Code Online (Sandbox Code Playgroud)

呼叫:

refCode = makeRefCode(e[c].lastname, cNo); cout << refCode;//Prints nothing
Run Code Online (Sandbox Code Playgroud)

功能定义:

string makeRefCode(string lastname, int cNo)
{
    string tStr;
    unsigned int i, j = 1;
    unsigned int x;
    x = lastname.length();
    tStr[0] = lastname[0];
    cout << tStr[0];//Prints correct value
    for (i = 1; i < x; i++)
    {
        if (!isVowel(toupper(lastname[i])))
        {
            //tStr[j] = lastname[i];//
            j++;
        }
    }
    //refCode[j] = '0'; // add string terminator
    return tStr;
}

bool isVowel(char aChar)
{
    switch (aChar) //<ctype>
    {
        case 'A':
        case 'E': 
        case 'I': 
        case 'O':
        case 'U': return true; break;
        default: return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

正如我已经尝试解决这个问题,我得到了断言,访问冲突和字符串错误,似乎它说字符串不够大.任何帮助将不胜感激.

jah*_*haj 5

字符串不会自动增大.如果你的字符串从零长度开始(就像tStr那样),那么你需要使用push_back在字符串末尾添加字符.

tStr.push_back(lastname[0]);
Run Code Online (Sandbox Code Playgroud)

tStr.push_back(lastname[i]);
Run Code Online (Sandbox Code Playgroud)