[英]Member reference base type is not a structure or a union
I need some help with a problem I'm having with my homework assignment.我需要一些帮助来解决我的家庭作业问题。
//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;
}
The error message is:错误信息是:
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();
~~~~~~~~~~~~~~~~~~~^~~~~~
Any help is much appreciated!任何帮助深表感谢!
Reading your code shows indicates that you are trying to call erase()
on a character in an std::string
.阅读您的代码表明您正在尝试对
std::string
中的字符调用erase()
。 If you wish to erase a character in a string, you need to call the erase()
function in std::string
.如果你想擦除字符串中的一个字符,你需要在
std::string
中调用erase()
function。 I recommend using iterators to loop through your string, since using the erase()
function on a string that you're iterating through with integer indices would be difficult, since the index of each character in the string changes every time you erase a character.我建议使用迭代器来遍历您的字符串,因为在您使用 integer 索引迭代的字符串上使用
erase()
function 会很困难,因为每次擦除字符时字符串中每个字符的索引都会改变。 Taking this into account, the inner for loop becomes:考虑到这一点,内部 for 循环变为:
for (auto it = current_sequence.begin(); it != current_sequence; ) {
if (*it == *(it + 1)) {
it = current_sequence.erase(it);
} else {
it++;
}
}
Another note is that if you plan on mutating the strings in the array you should probably be using a pointer or reference, since current_sequence
is just a copy of the string in the array.另一个注意事项是,如果您打算改变数组中的字符串,您可能应该使用指针或引用,因为
current_sequence
只是数组中字符串的副本。 Since you're already using an array, I recommend looping through the string pointers themselves, like so:由于您已经在使用数组,因此我建议您自己循环遍历字符串指针,如下所示:
for (std::string *current_sequence = sequences; current_sequence < sequences + sequences_length; current_sequence++) {
/* Do something with current_sequence */
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.