-1

I used get() and getline() to read the file then input to my map(). Then insert from map class to AVLTree and AVLNode class. What should I do to fix the const string error and where the exactly problem is? Thank you for all your time and help. Here is the whole code. https://github.com/RichardHung/AVL-BST-Tree.git

60 13 SequenceMap.cpp [Error] passing 'const std::basic_string' as 'this' argument of 'std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator]' discards qualifiers [-fpermissive]
[Note] void std::basic_string<_CharT, _Traits, _Alloc>::push_back(_CharT) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator]
[Note] no known conversion for argument 1 from 'const std::basic_string' to '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.
}
}

main.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( );
    }

}
Rizier123
  • 58,877
  • 16
  • 101
  • 156
Richard
  • 13
  • 3
  • 2
    Without knowing what `SequenceMap` is theres not much we can do. – Borgleader Dec 15 '14 at 19:28
  • A std::map is likely a tree (AVL, Red/Black, ...) –  Dec 15 '14 at 19:33
  • @DieterLücking I thought is tree like a sorting algorithm and map is a data storing as a node with key(element_1) and value(element_2)? – Richard Dec 15 '14 at 19:39
  • @Richard Are you passing three strings into that `insert` call? – caps Dec 15 '14 at 19:41
  • @caps when I pass to map, the 1st parameter is a const str "AVL". 2nd expect to be string but it gives me a vector. 3rd is string. then map would store the str into vector so the tree would have vector as 2nd datatype. – Richard Dec 15 '14 at 19:44
  • @Richard we can't help you without knowing more about the definition of the `SequenceMap`. – caps Dec 15 '14 at 19:51
  • @caps My bad. I pass the std::string as reference from main to map then to the tree class. Then it change to const when it pass to node class. the c++ lib use vector – Richard Dec 17 '14 at 03:12
  • @Borgleader Sorry my bad.Can you take a look on what make the std::string pass as const std::string? How should I fix it as std::string pass as std::string? – Richard Dec 17 '14 at 05:03
  • One of your edits took out the `map` declaration, along with a bunch of other variable declarations. I had to look at the edit history to see exactly what it was. – caps Dec 17 '14 at 05:06
  • @caps You mean the map's member declaration or function declaration? `template class SequenceMap { public: Comparable_1 key; Comparable_2 value; AvlTree < Comparable_1, Comparable_2 > avl; BinarySearchTree < Comparable_1, Comparable_2 > bst; SequenceMap(){}; ~SequenceMap(); }` – Richard Dec 17 '14 at 05:25
  • I think I solved your problem. See my answer. In the future I suggest that you learn how to make a [short, self-contained, correct example](http://www.sscce.org/). Many times just the process of doing so will show you what your problem is. – caps Dec 17 '14 at 05:44

1 Answers1

1

You need to pay attention to your compiler warnings.

In a couple of your classes you had std::vector<Comparable_2> members where I think you meant to have just plain old Comparable_2 members. I couldn't get your code to compile without changing that. As far as I can tell, that was your main issue. You can't just assign an object of type T to a container of type vector<T>. Not only are they different types with no implicit conversions according to the compiler, but such an assignment makes no logical sense.

You may have meant to use a std::vector<Comparable_2>. In that case your syntax is wrong in numerous places; you will need to do things like element2.push_back(y) instead of element2 = y. You'll also need to change your comparisons like element2 == y to something like std::find(element2.begin(), element2.end(), y) != element2.end(), because just like you can't assign a T to a vector<T>, you also won't be able to compare a T to a vector<T>.

However, you had quite a few significant compiler warnings: ambiguous if/else blocks, many functions not always returning a value, other functions never ever returning a value, etc. If changing your element2 members from std::vectors to std::strings still doesn't let your code compile, I'd have a look at fixing some of those warnings--several of them will probably lead to your app doing things you don't expect.

Here is a link to coliru compiling and running your app. I loaded the input file into this other coliru instance here.

As a side note, you really shouldn't do using namespace std;.

Community
  • 1
  • 1
caps
  • 1,225
  • 14
  • 24
  • So is that means avoid using vector here? – Richard Dec 17 '14 at 06:24
  • Thanks. I stop using vector to make the code simple. I will post the code after I finish it. The reason I use vector is that I think I can make it expendable if I want to insert something more. – Richard Dec 17 '14 at 06:38
  • You could still use vector, but you would have to change the syntax and logic to reflect such a change. The syntax and logic you currently have presumes the object is just a `Comparable_2` – caps Dec 17 '14 at 21:36