简体   繁体   English

成员引用基类型不是结构或联合

[英]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.

相关问题 成员参考库类型不是结构或联合 - Member Reference Base Type Not a Structure or Union 成员引用基类型“Point *”不是结构体或联合体? - Member reference base type 'Point *' is not a structure or union? 成员引用基本类型“ double”不是结构或联合 - Member reference base type 'double' is not a structure or union 成员引用基类型“int”不是结构或联合 - Member reference base type 'int' is not a structure or union 成员引用基本类型不是结构或联合 - Member reference base type is not a structure or union 成员引用基类型 &#39;std::vector<char> *[10]&#39; 不是结构体或联合体 - Member reference base type 'std::vector<char> *[10]' is not a structure or union 成员参考基础类型&#39;节点 <int> *&#39;不是结构或联盟 - Member Reference Base Type 'Node<int> *' Is Not A Structure Or Union 成员引用基类型“elem *”不是结构或联合 C++ - Member reference base type 'elem *' is not a structure or union C++ C ++成员参考基本类型&#39;Vertex * const&#39;不是结构或联合 - C++ Member Reference base type 'Vertex *const' is not a structure or union 实现观察者模式的问题:“成员引用基类型 ________ 不是结构或联合” - Problem Implementing Observer Pattern : “Member reference base type ________ is not a structure or union”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM