如何更正此分配错误

How do I correct this allocation error

本文关键字:错误 分配 何更正      更新时间:2023-10-16

我假设创建一个嵌套循环,循环遍历动态分配的数组(故事)找到字符"*",并用来自其他动态分配数组(user_input)的信息填充该字符所在的位置。然后将此替换信息存储在一个名为 body to cout 的新动态分配数组中。这是错误:

1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              _Alloc=std::allocator<char>
1>          ]
There is no context in which this conversion is possible

谁能告诉我我做错了什么?这是我的代码:

void create_story(string & user_inputs, string *story, int num_lines,
        string **body)
{
    *body = new string[num_lines];
    for (int i = 0; i < story[i].length; i++)
    {
        if (story[i] == "*")
        {
            body[i];
        }
        for (int j = 0; j < story[i].length(); j++)
        {
            if (story[i][j] == '*')
            {
                cout << "I found it at character number: " << i;
                *body[i] += user_inputs;
            }
            else
            {
                body[i] += story[i][j];
            }
        }
    }
}

此代码中存在多个逻辑错误。

  1. 以下行对程序的状态没有影响(没有副作用)。

    body[i];

  2. 以下行极不可能正确终止,因为指数i出现在不等式的两侧。

    for (int i=0; i<story[i].length; i++)

  3. 上面的行可能无法编译,因为它比较了一个int和一个成员函数(length)。

  4. 下面的行测试整个第i条线是否是简单的一颗星。

    if(story[i]=="*")

我相信还有其他问题。您应该从较少的代码开始,看看是否可以分步完成分配,而不是尝试一次完成所有操作。它会更容易理解。