Really weird strings problem.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gasfusion
    New Member
    • Sep 2006
    • 59

    Really weird strings problem.

    I wrote a class Min-Max Heap Template Class which works perfectly fine with integers. As part of this data structure, i have to implement some sort of method to check for the smallest children/grandchildren of any given position in the Min-Max Heap array. Part of this method retrieves an item from my MinMaxHeap array at a specific position and then compare it to NULL. This works fine with integers. However i cannot(!) compare strings to NULL. I receive 100+ errors among the following lines:
    Code:
    Error	1	error C2784: 'bool std::operator ==(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const _Elem *' from 'int'	c:\documents and settings\dbm\my documents\visual studio 2005\projects\minmaxheap\minmaxheap\minmaxheap.cpp	261
    Now, the compiler does this check at compile-time. Not at run-time. So checking if a template is a string or an integer is out of the question. Can i use anything else to fix this problem???
    Here is my Function
    Code:
    template <class Type>
    const int MinMaxHeap<Type>::findMinChild(const int pos)
    {
        /* Get left and right children of current position */
        int lChildPos = getLChild(pos);
        int rChildPos = getRChild(pos);
    
        /* Get items at left and right positions */
        Type itemAtLeft = getElementAtPos(lChildPos);
        Type itemAtRight = getElementAtPos(rChildPos);
    
    	if (typeid(itemAtLeft) == typeid(string))
    	{
    		if (itemAtLeft.size() == 0 || itemAtRight.size() == 0 || itemAtLeft < itemAtRight)
    			return lChildPos;
    		return rChildPos;
    	}
    	else
    	{
    		/* Compare both children and return the smallest */
    		compCount++;
    		if (itemAtLeft == NULL || itemAtRight == NULL || itemAtLeft < itemAtRight)
    			return lChildPos;
    		return rChildPos;
    	}
    }
    As you can see i am attempting to check the template argument for strings, which of course doesn't work. So i am looking for some sort of other way to check both strings and integers for null :(
    Any help is appreciated!
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by gasfusion
    I wrote a class Min-Max Heap Template Class which works perfectly fine with integers. As part of this data structure, i have to implement some sort of method to check for the smallest children/grandchildren of any given position in the Min-Max Heap array. Part of this method retrieves an item from my MinMaxHeap array at a specific position and then compare it to NULL. This works fine with integers. However i cannot(!) compare strings to NULL. I receive 100+ errors among the following lines:
    Code:
    Error	1	error C2784: 'bool std::operator ==(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const _Elem *' from 'int'	c:\documents and settings\dbm\my documents\visual studio 2005\projects\minmaxheap\minmaxheap\minmaxheap.cpp	261
    Now, the compiler does this check at compile-time. Not at run-time. So checking if a template is a string or an integer is out of the question. Can i use anything else to fix this problem???
    Here is my Function
    Code:
    template <class Type>
    const int MinMaxHeap<Type>::findMinChild(const int pos)
    {
        /* Get left and right children of current position */
        int lChildPos = getLChild(pos);
        int rChildPos = getRChild(pos);
    
        /* Get items at left and right positions */
        Type itemAtLeft = getElementAtPos(lChildPos);
        Type itemAtRight = getElementAtPos(rChildPos);
    
    	if (typeid(itemAtLeft) == typeid(string))
    	{
    		if (itemAtLeft.size() == 0 || itemAtRight.size() == 0 || itemAtLeft < itemAtRight)
    			return lChildPos;
    		return rChildPos;
    	}
    	else
    	{
    		/* Compare both children and return the smallest */
    		compCount++;
    		if (itemAtLeft == NULL || itemAtRight == NULL || itemAtLeft < itemAtRight)
    			return lChildPos;
    		return rChildPos;
    	}
    }
    As you can see i am attempting to check the template argument for strings, which of course doesn't work. So i am looking for some sort of other way to check both strings and integers for null :(
    Any help is appreciated!
    Code:
    could not deduce template argument for 'const _Elem *' from 'int'
    .

    Have you tryed using 'int*'?


    Savage

    Comment

    • gasfusion
      New Member
      • Sep 2006
      • 59

      #3
      I honestly don't understand what '_const Elem*' even means. I'm not passing any template argument pointers, neither do any of my functions return any. Apparently this problem has nothing to do with comparing strings to NULL. After doing some research, i found out that for some reason the compiler cannot determine the Type of a function so you have to pass it explicitly Like:
      foo<Type>(param 1, param2), which in turn says something like "Illegal use of Type" or something similar to that. Either way, it doesn't work.
      This is really bugging me out. I have never seen this problem before.

      When you ask if i tried 'int*' where in the code should i try this?
      Thank you.

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

        #4
        Originally posted by gasfusion
        I honestly don't understand what '_const Elem*' even means. I'm not passing any template argument pointers, neither do any of my functions return any. Apparently this problem has nothing to do with comparing strings to NULL. After doing some research, i found out that for some reason the compiler cannot determine the Type of a function so you have to pass it explicitly Like:
        foo<Type>(param 1, param2), which in turn says something like "Illegal use of Type" or something similar to that. Either way, it doesn't work.
        This is really bugging me out. I have never seen this problem before.

        When you ask if i tried 'int*' where in the code should i try this?
        Thank you.
        NULL it self is setting a pointer to point toward null.Int which you use to determine childs is not a pointer ,but int* is one.Strings them selfs are array of chars where it's
        name point to location.So setting and comparing string with NULL should not
        make a problem.This
        Code:
        could not deduce template argument for 'const _Elem *' from 'int'
        is probably making a problem because it's of const
        type and such as it is it's not allowing any change of his value and make a compile prob when compiling.

        One of solution might be,as you said,explicit but right now I don't know why is
        it reporting invalid use of type??

        Savage

        Comment

        • gasfusion
          New Member
          • Sep 2006
          • 59

          #5
          Not a single clue. I've tried pretty much everything i could. The only thing left is to actually overload the equality operator (==), since that's where it seems to fail. I still don't fully understand what the heck's causing this and why the template args aren't passed. It's almost as if strings don't have == defined lol. Which is just bogus.

          Comment

          • gasfusion
            New Member
            • Sep 2006
            • 59

            #6
            Ok i guess i can call this *RESOLVED*. The problem was not the == operator, it was comparing strings to NULL which returns the whole shibang of error messages. So i'm just comparing template args to string and compare it to "" if it's a string.
            Thanks for the help.

            Comment

            • Savage
              Recognized Expert Top Contributor
              • Feb 2007
              • 1759

              #7
              Originally posted by gasfusion
              Ok i guess i can call this *RESOLVED*. The problem was not the == operator, it was comparing strings to NULL which returns the whole shibang of error messages. So i'm just comparing template args to string and compare it to "" if it's a string.
              Thanks for the help.

              I wasn't much of help,but im glad it's *RESOLVED* !!!!



              Savage

              Comment

              Working...