如何更正此分配错误
How do I correct this allocation error
我假设创建一个嵌套循环,循环遍历动态分配的数组(故事)找到字符"*",并用来自其他动态分配数组(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];
}
}
}
}
此代码中存在多个逻辑错误。
-
以下行对程序的状态没有影响(没有副作用)。
body[i];
-
以下行极不可能正确终止,因为指数
i
出现在不等式的两侧。for (int i=0; i<story[i].length; i++)
-
上面的行可能无法编译,因为它比较了一个
int
和一个成员函数(length
)。 -
下面的行测试整个第i条线是否是简单的一颗星。
if(story[i]=="*")
我相信还有其他问题。您应该从较少的代码开始,看看是否可以分步完成分配,而不是尝试一次完成所有操作。它会更容易理解。
相关文章:
- vector.resize()中的分配错误
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 正在理解智能指针,但出现错误:未分配正在释放的指针
- 如何修复只读对象中成员的错误分配?
- 无法将错误分配给成员函数中"类型的实体"
- 分段错误分配函数结果
- 如何实现,错误分配中止而不是抛出异常
- C++代码错误分配
- 如何修复错误分配C++运行时错误
- 分段错误 - 分配类困难
- 带有数组类型表达式的错误分配
- Sprintf 缓冲区问题,错误分配给字符数组
- 而 (p=strtok(NULL, "," )) 警告:p可识别的错误分配
- 错误:分配只读位置“arr2.IntArray::operator[](1)”arr2[1]=24;
- std::映射错误分配错误
- 为什么"e.what()"打印"错误分配"?
- std::vector<const char *>的错误分配效果?
- 使用指向类的指针进行错误分配
- 错误:分配只读引用“__b”
- 向量错误分配错误 C++