成员引用基类型不是结构或联合
Member reference base type is not a structure or a union
我的家庭作业遇到了一个问题,需要一些帮助。
//finds frequency of a sequence in an array (including mutations)
int find_freq_with_mutations(string target,string sequences[],int sequences_length) {
for (int i = 0; i < sequences_length; i++) { //goes through each sequence in array
string current_sequence = sequences[i]; //sets current sequence as a string
for (int j = 0; j < current_sequence.length(); j++) { //iterate for every character in sequence
if (current_sequence[j] == current_sequence[j+1]) {
current_sequence[j].erase();
}
}
}
int target_frequency = find_frequency(target, sequences, sequences_length);
return target_frequency;
}
错误消息为:
DNA_sequencing.cpp:70:24: error: member reference base type
'std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> >::value_type' (aka 'char') is not a structure or
union
current_sequence[j].erase();
~~~~~~~~~~~~~~~~~~~^~~~~~
非常感谢您的帮助!
阅读代码表明您正试图对std::string
中的字符调用erase()
。如果要擦除字符串中的字符,则需要调用std::string
中的erase()
函数。我建议使用迭代器在字符串中循环,因为在使用整数索引进行迭代的字符串上使用erase()
函数会很困难,因为每次擦除字符时,字符串中每个字符的索引都会发生变化。考虑到这一点,内部for循环变为:
for (auto it = current_sequence.begin(); it != current_sequence; ) {
if (*it == *(it + 1)) {
it = current_sequence.erase(it);
} else {
it++;
}
}
另一个注意事项是,如果您计划更改数组中的字符串,则可能应该使用指针或引用,因为current_sequence
只是数组中字符串的副本。由于您已经在使用数组,我建议您循环使用字符串指针本身,如下所示:
for (std::string *current_sequence = sequences; current_sequence < sequences + sequences_length; current_sequence++) {
/* Do something with current_sequence */
}
相关文章:
- 使用对const结构的const数组的引用进行构造
- 为什么将一个结构的引用设置为等于另一个结构只会更改一个数据成员?
- 如何传递带有通过引用传递的结构参数的函数?
- 具有引用成员的结构是否具有唯一的对象表示形式
- 设计将引用元素移动到开头的数据结构.C++
- 在 c++ 中将变量作为结构构造函数中的引用传递
- 对结构方法的未定义引用
- 为什么结构化绑定不使用"auto&"返回对结构成员的引用,而是返回成员本身
- C++-将具有引用的长参数列表重构为结构
- 在结构中使用 switch 引用结构中的数据C++
- 如何防止引用返回的私有结构的突变
- 取消引用 void 指针以将值分配给结构
- 使用 sscanf 设置结构引用的值
- 结构引用编译错误:预期的标识符或"&"标记之前的"("
- Boost Intervocess:通过迭代通过从结构引用对象的映射进行迭代时
- 结构引用和取消引用运算符
- 设置数据结构引用
- 在 OpenGL 中呈现三角形结构引用数组
- 正在将常量结构引用转换为非常量指针
- C++中结构引用的默认参数