C++ 将字符串插入地图时出现编译错误

C++ Getting compilation error while insert string into a map

本文关键字:编译 错误 地图 字符串 插入 C++      更新时间:2023-10-16

我使用get()getline()来读取文件,然后输入到我的map()。然后从地图类插入到AVLTreeAVLNode类。我应该怎么做才能解决常量字符串错误以及问题到底在哪里?感谢您的所有时间和帮助。这是整个代码。https://github.com/RichardHung/AVL-BST-Tree.git

60 13 SequenceMap.cpp [错误] 将 'const std::basic_string' 作为 'std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::

operator=(const std::basic_string<_CharT, _Traits, _Alloc>&( [使用 _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator] 的 'this' 参数丢弃限定符 [-permissive]
[注意] void std::basic_string<_CharT, _Traits, _Alloc>::p ush_back(_CharT( [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator]
[注意] 参数 1 没有已知的从 'const std::basic_string' 到 'char' 的转换

template <typename Comparable_1, typename Comparable_2>
class AvlNode
{
  Comparable_1 element1; // sequence
  vector<Comparable_2> element2; // enzy
  AvlNode *left;
  AvlNode *right;
  int height;
  bool exist;
  AvlNode( const Comparable_1 & theElement1, const Comparable_2 & theElement2, AvlNode *lt, AvlNode *rt, int h = 0, bool ex = false): 
  element1( theElement1 ),element2( theElement2 ), left( lt ), right( rt ), height( h ), exist( ex ) { }
  friend class AvlTree<Comparable_1, Comparable_2>;
};
template <typename Comparable_1, typename Comparable_2>
void AvlTree<Comparable_1, Comparable_2>::insert( const Comparable_1 & x, const Comparable_2 y, AvlNode<Comparable_1, Comparable_2> * & t )
{
 if( t == NULL )
     t = new AvlNode<Comparable_1, Comparable_2>( x, y, NULL, NULL, true );
 else if( x < t->element1 )
 {
      insert( x, y, t->left );
      if( height( t->left ) - height( t->right ) == 2 )
          if( x < t->left->element1 )
              rotateWithLeftChild( t );
          else
              doubleWithLeftChild( t );
 }
 else if( x > t->element1 )
 {
      insert( x, y, t->right );
      if( height( t->right ) - height( t->left ) == 2 )
          if( x < t->right->element1 )
              rotateWithRightChild( t );
          else
              doubleWithRightChild( t );
 }
 else // ( x == t->element1 )
 {
     t->element2 = y ;
 }
 t->height = max( height( t->left ), height( t->right ) ) + 1;
 }
template <typename Comparable_1, typename Comparable_2>
class SequenceMap
{
  public:
         Comparable_1 key;                      
         Comparable_2 value;            
         AvlTree < Comparable_1, Comparable_2 > avl;
         BinarySearchTree < Comparable_1, Comparable_2 > bst;
         SequenceMap(){};
         ~SequenceMap();
         }
template <typename Comparable_1, typename Comparable_2>
void SequenceMap<Comparable_1, Comparable_2>::AVLinsert( const Comparable_1 & str, const Comparable_2 & set )
{
if ( avl.isEmpty( ) )        
{
    key = str;
    value = set;
    avl.insert( key, value );
}   
else                        // check the enzy is duplicated or not
{
    for( int i=0; i<set.size( ); i++ )
    {
        if ( avl.findEnzyme( key, set ) ) // if cannot find the enzy in the node
        {
                merge( set );
                avl.insert( key, value ); 
        } 
    }  
    // if find the same enzy in the node, do nothing to avoid duplicated.
}
}

主.cpp

while ( ! fin.eof( ) )
{           
    if ( fin.peek( ) == '/' )
    {
        fin.get();      // skip "/" and move ptr to next sequence
        while ( fin.peek() != 'n' ) 
        {
            getline(fin, sequence, '/');
            cout << "enzyme: " << enzyme << endl;
            cout << "sequence: " << sequence << endl;
            map.insert(tree, sequence, enzyme);
            sequence.clear();
            fin.get();  // skip "/" and move ptr to next sequence
            if(fin.peek() == '/')
                fin.get();                  
        }
        fin.get();
        enzyme.clear();
    }
    else // if( enz != '/' )
    {
        enzyme += fin.get( );
    }
}

您需要注意编译器警告。

在你的几个班级中,你有std::vector<Comparable_2>成员,我想你的意思是只有普通的老Comparable_2成员。如果不更改它,我就无法编译您的代码。据我所知,这是你的主要问题。不能只将 T 类型的对象分配给类型 vector<T> 的容器。根据编译器,它们不仅是不同的类型,没有隐式转换,而且这样的赋值在逻辑上没有意义。

您可能打算使用std::vector<Comparable_2> .在这种情况下,您的语法在很多地方都是错误的;您将需要执行诸如element2.push_back(y)而不是element2 = y之类的操作。您还需要将比较(如element2 == y(更改为类似std::find(element2.begin(), element2.end(), y) != element2.end(),因为就像您无法为vector<T>分配T一样,您也无法将Tvector<T>进行比较。

但是,您有很多重要的编译器警告:不明确的if/else块,许多函数并不总是返回值,其他函数从不返回值,等等。如果将element2成员从 std::vectors 更改为 std::strings 仍然无法编译代码,我会考虑修复其中的一些警告 - 其中一些可能会导致您的应用程序执行您意想不到的事情。

这是指向 coliru 编译和运行应用程序的链接。我在此处将输入文件加载到另一个 coliru 实例中。

作为旁注,你真的不应该做using namespace std; .