title: Strange QT Unsigned Int Error
body: <p>I am working on an RPN calculator, and right now my main.cpp along with these other class files are supposed to take in a string of algebraic operations e.g. "3 + 4 / 2.34" that can take arithmetic operators and can read in doubles, ints, fractions, and mixed fractions (an integer, a single space, then a fraction). </p>
  
  <p>In the command console I am getting this error: </p>
  
  <pre><code>ASSERT: "uint(i) &lt; uint(size())" in file c:\QtSDK\Desktop\Qt\4.8.1\mingw\include
  /QtCore/qstring.h, line 701
  
  This application has requested the Runtime to terminate it in an unusual way.
  Please contact the application's support team for more information.
  Press &lt;RETURN&gt; to close this window...
  </code></pre>
  
  <p>I don't understand QT well enough to be able to sniff out where this error is coming from, so I see no other option than to show my code and hope someone can help me out. There's a lot of code :/ I have excluded the headers and sources for the Fraction and Mixed classes, as those do not utilize qstring.h which is what the compiler seems to be complaining about. If you think these are required I can post them. </p>
  
  <p>As far as I can tell it has something to do with comparing an unsigned integer to an integer, possibly trying to assign a negative int to an uint? Here's the bizarre part, in the main.cpp file, the input string is "1345/43143 - 2" which leads to an error. Also, "3+4" leads to an error as well. The weird part is, if I set the input string to "2341 + (23 ^ 4.421) / 23/321 - 5 6/7" it DOES work but no other input string seems to work. </p>
  
  <p>Token.h</p>
  
  <pre><code>#ifndef TOKEN_H
  #define TOKEN_H
  
  #include "Mixed.h"
  #include "Fraction.h"
  
  class Token
  {
  public:
      Token();
      Token(const QString&amp; Substring);
      void SetIntPart(const QString&amp; Substring);
      void SetDoublePart(const QString&amp; Substring);
      void SetFractionPart(const QString&amp; Substring);
      void SetMixedPart(const QString&amp; Substring);
      void SetOperatorPart(const QString&amp; Substring);
  
      bool isDouble(const QString&amp; Substring);
      bool isInt(const QString&amp; Substring);
      bool isFraction(const QString&amp; Substring);
      bool isMixed(const QString&amp; Substring);
      bool isOperator(const QString&amp; Substring);
      bool isNumber()
      { return (inttoken || doubletoken || fractiontoken || mixedtoken); }
      bool isLeftParen();
      bool isRightParen();
      bool isOperator();
  
  
      int IntPart() { return intpart; }
      double DoublePart() { return doublepart; }
      Fraction FractionPart() { return fractionpart; }
      Mixed MixedPart() { return mixedpart; }
      char OperatorPart() { return operatorpart; }
      bool IntToken() { return inttoken; }
      bool DoubleToken() { return doubletoken; }
      bool FractionToken() { return fractiontoken; }
      bool MixedToken() { return mixedtoken; }
      bool OperatorToken() { return operatortoken; }
      QString toQString();
      void Print(ostream&amp; out) const;
      friend ostream&amp; operator&lt;&lt;(ostream&amp; out, const Token&amp; T);
  
  private:
      int intpart;
      double doublepart;
      Fraction fractionpart;
      Mixed mixedpart;
      char operatorpart;
  
      bool inttoken;
      bool doubletoken;
      bool fractiontoken;
      bool operatortoken;
      bool mixedtoken;
  
      void ClearBools();
      void ClearParts();
      void ClearAll() { ClearBools(); ClearParts(); }
  };
  
  
  
  #endif // TOKEN_H
  </code></pre>
  
  <p>Parser.h</p>
  
  <pre><code>#ifndef PARSER_H
  #define PARSER_H
  
  #include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include "Fraction.h"
  #include "Mixed.h"
  #include "Queue.h"
  #include "Stack.h"
  #include "Token.h"
  
  class Parser
  {
  public:
      Parser();
      void LoadInputQueue(const QString&amp; QS);
      void LoadOutputQueue();
      void CopyInputToString(QString &amp;QS);
      void CopyOutputToString(QString &amp;QS);
      bool isOperator(const QChar&amp; Qch);
      bool isSpace(const QChar&amp; Qch);
      bool isRightParen(const QChar&amp; Qch);
      bool isLeftParen(const QChar&amp; Qch);
      bool isParen(const QChar&amp; Qch);
      bool isDigit(const QChar&amp; Qch);
      void PrintInputQueue(ostream&amp; out);
      void PrintOutputQueue(ostream&amp; out);
      void PrintOperatorStack(ostream&amp; out);
      int precedence(Token T);
  
  private:
      Queue&lt;Token&gt; InputQueue;
      Queue&lt;Token&gt; OutputQueue;
      Stack&lt;Token&gt; OperatorStack;
  
      void Tokenize(const QString&amp; InputString, int n);
  };
  
  #endif // PARSER_H
  </code></pre>
  
  <p>main.cpp</p>
  
  <pre><code>#include &lt;QtCore/QCoreApplication&gt;
  #include &lt;cstdlib&gt;
  #include &lt;iostream&gt;
  #include "Token.h"
  #include "Parser.h"
  
  using namespace std;
  
  int main(int argc, char *argv[])
  {
      QCoreApplication a(argc, argv);
  
      /*cout &lt;&lt; "Fractiontoken: " &lt;&lt; A.FractionToken() &lt;&lt; endl;
      cout &lt;&lt; "Fraction part: " &lt;&lt; A.FractionPart() &lt;&lt; endl;
      cout &lt;&lt; "Inttoken: " &lt;&lt; A.IntToken() &lt;&lt; endl;
      cout &lt;&lt; "Int part: " &lt;&lt; A.IntPart() &lt;&lt; endl;
      cout &lt;&lt; "Doubletoken: " &lt;&lt; A.DoubleToken() &lt;&lt; endl;
      cout &lt;&lt; "Double part: " &lt;&lt; A.DoublePart() &lt;&lt; endl;
      cout &lt;&lt; "Mixedtoken: " &lt;&lt; A.MixedToken() &lt;&lt; endl;
      cout &lt;&lt; "Mixed part: " &lt;&lt; A.MixedPart() &lt;&lt; endl;
      cout &lt;&lt; "Operatortoken: " &lt;&lt; A.OperatorToken() &lt;&lt; endl;
      cout &lt;&lt; "Operator part: " &lt;&lt; A.OperatorPart() &lt;&lt; endl;*/
  
      QString inputstring = "1345/43143 - 2";
      Parser P;
      QString displaystring;
  
      P.LoadInputQueue(inputstring);
      P.CopyInputToString(displaystring);
      cout &lt;&lt; qPrintable(displaystring);
      cout &lt;&lt; endl;
  
      P.LoadOutputQueue();
      P.CopyOutputToString(displaystring);
      cout &lt;&lt; qPrintable(displaystring);
      cout &lt;&lt; endl;
  
      return a.exec();
  }
  </code></pre>
  
  <p>parser.cpp</p>
  
  <pre><code>#include "Parser.h"
  enum PARENTHESIS_1{P1};
  enum PARENTHESIS_2{P2};
  
  bool Parser::isOperator(const QChar&amp; Qch)
  {
      char ch = Qch.toAscii();
      if (ch == '+' ||
              ch == '-' ||
              ch == '*' ||
              ch == '/' ||
              ch == '(' ||
              ch == ')' ||
              ch == '^' )
          return 1;
      return 0;
  }
  
  bool Parser::isSpace(const QChar&amp; Qch)
  {
      if (Qch.toAscii() == ' ')
          return 1;
      return 0;
  }
  
  bool Parser::isLeftParen(const QChar&amp; Qch)
  {
      char ch = Qch.toAscii();
      if (ch == '(')
          return 1;
      return 0;
  }
  
  bool Parser::isRightParen(const QChar&amp; Qch)
  {
      char ch = Qch.toAscii();
      if (ch == ')')
          return 1;
      return 0;
  }
  
  bool Parser::isParen(const QChar&amp; Qch)
  {
      if (isRightParen(Qch) || isLeftParen(Qch))
          return 1;
      return 0;
  }
  
  bool Parser::isDigit(const QChar &amp;Qch)
  {
      char ch = Qch.toAscii();
      if (ch &gt; 47 &amp;&amp; ch &lt; 58)
          return 1;
      return 0;
  }
  
  
  Parser::Parser() : InputQueue(100), OutputQueue(100)
  {
  
  }
  
  void Parser::Tokenize(const QString&amp; InputString, int n)
  {
      if (n &gt;= InputString.size())
          return;
      else
      {
          if (isOperator(InputString[n]))
          {
              QString temp;
              temp.append(InputString[n]);
              Token temptoken(temp);
              InputQueue.enqueue(temptoken);
              Tokenize(InputString, n+1);
          }
          else if (isSpace(InputString[n]))
              Tokenize(InputString, n+1);
          else
          {
              QString temp;
              while (n &lt; InputString.size() &amp;&amp; !isSpace(InputString[n]) &amp;&amp; !isParen(InputString[n]))
              {
                  temp.append(InputString[n]);
                  n++;
              }
              if (isSpace(InputString[n]))
              {
                  if (isDigit(InputString[n+1]))
                  {
                      temp.append(' ');
                      n++;
                      while (n &lt; InputString.size() &amp;&amp; !isSpace(InputString[n]) &amp;&amp;
                                      !isParen(InputString[n]))
                      {
                          temp.append(InputString[n]);
                          n++;
                      }
                      Token temptoken(temp);
                      InputQueue.enqueue(temptoken);
                      Tokenize(InputString, n);
                  }
                  else if (isOperator(InputString[n+1]))
                  {
                      Token temptoken(temp);
                      InputQueue.enqueue(temptoken);
                      Tokenize(InputString, n+1);
                  }
              }
              else if (isParen(InputString[n]))
              {
                  Token temptoken(temp);
                  InputQueue.enqueue(temptoken);
                  temp.clear();
                  temp.append(InputString[n]);
                  Token temptoken2(temp);
                  InputQueue.enqueue(temptoken2);
                  Tokenize(InputString, n+1);
              }
  
          }
  
  
       }
  }
  
  int Parser::precedence(Token T)
  {
      if (T.OperatorToken())
      {
          char temp = T.OperatorPart();
          if (temp == '^')
              return 4;
          if (temp == '*' || temp == '/')
              return 3;
          if (temp == '+' || temp == '-')
              return 2;
      }
      return 1;
  }
  
  void Parser::LoadOutputQueue()
  {
      while (!InputQueue.empty())
      {
          Token temptoken;
          InputQueue.dequeue(temptoken);
          if (temptoken.isNumber())
              OutputQueue.enqueue(temptoken);
          else if (temptoken.isLeftParen())
              OperatorStack.push(temptoken);
          else if (temptoken.isRightParen())
          {
              while (!OperatorStack.empty() &amp;&amp; !(OperatorStack.Peek()).isLeftParen())
              {
                  Token temp;
                  OperatorStack.pop(temp);
                  OutputQueue.enqueue(temp);
              }
              if (!OperatorStack.empty() &amp;&amp; (OperatorStack.Peek()).isLeftParen())
                  OperatorStack.pop(temptoken);
              else if (OperatorStack.empty())
                  throw P1;
          }
          else if (temptoken.isOperator())
          {
              while (!OperatorStack.empty() &amp;&amp; precedence(temptoken) &lt; precedence(OperatorStack.Peek()))
              {
                  Token temp;
                  OperatorStack.pop(temp);
                  OutputQueue.enqueue(temp);
              }
              OperatorStack.push(temptoken);
          }
  
      }
      while (!OperatorStack.empty())
      {
          Token temptoken;
          OperatorStack.pop(temptoken);
          if (temptoken.isLeftParen())
              throw P2;
          OutputQueue.enqueue(temptoken);
      }
  
  
  }
  
  //this function takes a QString (of chars) and from that builds its queue of tokens
  void Parser::LoadInputQueue(const QString &amp;QS)
  {
      Tokenize(QS,0);
  }
  
  void Parser::CopyOutputToString(QString &amp;QS)
  {
      QString tempstring;
      Token temptoken;
      for (int i=OutputQueue.Size()-1; i&gt;=0; i--)
      {
          temptoken = OutputQueue.Element(i);
          tempstring.append(temptoken.toQString());
      }
      QS = tempstring;
  
  }
  
  void Parser::CopyInputToString(QString&amp; QS)
  {
      QString tempstring;
      Token temptoken;
      for (int i=InputQueue.Size()-1; i&gt;=0; i--)
      {
          temptoken = InputQueue.Element(i);
          tempstring.append(temptoken.toQString());
      }
      QS = tempstring;
  }
  
  void Parser::PrintInputQueue(ostream&amp; out)
  {
      out &lt;&lt; InputQueue &lt;&lt; endl;
  }
  
  void Parser::PrintOutputQueue(ostream&amp; out)
  {
      out &lt;&lt; OutputQueue &lt;&lt; endl;
  }
  
  void Parser::PrintOperatorStack(ostream&amp; out)
  {
      out &lt;&lt; OperatorStack &lt;&lt; endl;
  }
  </code></pre>
  
  <p>token.cpp</p>
  
  <pre><code>#include "Token.h"
  #include &lt;QStringList&gt;
  
  enum TOKEN_EXCEPTIONS{UNKNOWN_TOKEN};
  
  Token::Token()
  {
  
  }
  
  Token::Token(const QString&amp; Substring)
  {
      if (isInt(Substring))
          SetIntPart(Substring);
      else if (isDouble(Substring))
          SetDoublePart(Substring);
      else if (isFraction(Substring))
          SetFractionPart(Substring);
      else if (isMixed(Substring))
          SetMixedPart(Substring);
      else if (isOperator(Substring))
          SetOperatorPart(Substring);
      else
      {
          cout &lt;&lt; qPrintable(Substring) &lt;&lt; endl;
          throw UNKNOWN_TOKEN;
      }
  }
  
  void Token::SetIntPart(const QString&amp; Substring)
  {
      ClearAll();
      intpart = Substring.toInt();
      inttoken = 1;
  }
  
  void Token::SetDoublePart(const QString&amp; Substring)
  {
      ClearAll();
      doublepart = Substring.toDouble();
      doubletoken = 1;
  
  }
  
  void Token::SetFractionPart(const QString&amp; Substring)
  {
      ClearAll();
      QStringList Q;
      Q = Substring.split('/');
      Fraction frac(Q[0].toInt(),Q[1].toInt());
      fractionpart = frac;
      fractiontoken = 1;
  
  }
  
  void Token::SetMixedPart(const QString&amp; Substring)
  {
      ClearAll();
      QStringList Q1, Q2;
      Q1 = Substring.split(' ');
      QString wholepart = Q1[0];
      Q2 = Q1[1].split('/');
      QString numeratorstring = Q2[0];
      QString denominatorstring = Q2[1];
      Mixed M(wholepart.toInt(),numeratorstring.toInt(),denominatorstring.toInt());
      mixedpart = M;
      mixedtoken = 1;
  }
  
  void Token::SetOperatorPart(const QString&amp; Substring)
  {
      ClearAll();
      operatorpart = Substring[0].toAscii();
      operatortoken = 1;
  }
  
  bool Token::isDouble(const QString&amp; Substring)
  {
      int pointcount=0;
      int intcount=0;
      int pointloc=0;
  
      for (int i=0; i&lt;Substring.size(); i++)
      {
          if (isdigit(Substring[i].toAscii()))
              intcount++;
          if (Substring[i].toAscii() == '.')
          {
              pointcount++;
              pointloc = i;
          }
      }
  
      if (pointcount != 1)
          return 0;
      if (intcount != Substring.size() - 1)
          return 0;
      return 1;
  
  }
  
  bool Token::isInt(const QString&amp; Substring)
  {
      if (Substring[0].toAscii() == '0')
          return 0;
  
      for (int i=0; i&lt;Substring.size(); i++)
      {
          if (!isdigit(Substring[i].toAscii()))
              return 0;
      }
  
      return 1;
  }
  
  bool Token::isFraction(const QString&amp; Substring)
  {
      int slashcount=0;
      int intcount=0;
      int slashloc=0;
  
      for (int i=0; i&lt;Substring.size(); i++)
      {
          if (Substring[i].toAscii() == '/')
          {
              slashcount++;
              slashloc = i;
          }
          if (isdigit(Substring[i].toAscii()))
              intcount++;
      }
  
      if (slashcount != 1) // if there is not exactly 1 slash in substring
          return 0;
      if (intcount != Substring.size() - 1) //if the rest of the chars are not integers
          return 0;
      if (slashloc == 0 || slashloc == Substring.size() - 1) //if slash is at wrong location
          return 0;
      if (Substring[slashloc+1].toAscii() == '0' ||
              Substring[0].toAscii() == '0')
          return 0;
      return 1;
  }
  
  bool Token::isMixed(const QString&amp; Substring)
  {
      if (Substring[0].toAscii() == '0')
          return 0;
  
      int spacecount=0;
      int slashcount=0;
      int intcount=0;
      int slashloc=0;
      int spaceloc=0;
  
      for (int i=0; i&lt;Substring.size(); i++)
      {
          if (isspace(Substring[i].toAscii()))
          {
              spacecount++;
              spaceloc = i;
          }
          if (isdigit(Substring[i].toAscii()))
              intcount++;
          if (Substring[i].toAscii() == '/')
          {
              slashcount++;
              slashloc = i;
          }
      }
  
  //    cout &lt;&lt; "spacecount: " &lt;&lt; spacecount &lt;&lt; endl;
  //    cout &lt;&lt; "slashcount: " &lt;&lt; slashcount &lt;&lt; endl;
  //    cout &lt;&lt; "intcount: " &lt;&lt; intcount &lt;&lt; endl;
  //    cout &lt;&lt; "slashloc: " &lt;&lt; slashloc &lt;&lt; endl;
  //    cout &lt;&lt; "spaceloc: " &lt;&lt; spaceloc &lt;&lt; endl;
  
      if (spacecount != 1)
          return 0;
      if (slashcount != 1)
          return 0;
      if (intcount != Substring.size() - 2)
          return 0;
      if (slashloc == 0 || slashloc == Substring.size()-1)
          return 0;
      if (spaceloc == 0 || spaceloc == Substring.size()-1)
          return 0;
      if (slashloc &lt; spaceloc)
          return 0;
      if (!isdigit(Substring[slashloc-1].toAscii()) || !isdigit(Substring[slashloc+1].toAscii()))
          return 0;
      if (!isdigit(Substring[spaceloc-1].toAscii()) || !isdigit(Substring[slashloc+1].toAscii()))
          return 0;
      return 1;
  
  }
  
  bool Token::isOperator(const QString&amp; Substring)
  {
      if (Substring.size() != 1)
          return 0;
      if (Substring[0] == '+' ||
              Substring[0] == '-' ||
              Substring[0] == '*' ||
              Substring[0] == '^' ||
              Substring[0] == '/' ||
              Substring[0] == '(' ||
              Substring[0] == ')')
          return 1;
      return 0;
  
  }
  
  void Token::ClearBools()
  {
      mixedtoken = 0;
      inttoken = 0;
      doubletoken = 0;
      fractiontoken = 0;
      operatortoken = 0;
  
  }
  
  void Token::ClearParts()
  {
      intpart = 0;
      doublepart = 0;
      Fraction f(0);
      fractionpart = f;
      Mixed m(0);
      mixedpart = m;
      operatorpart = NULL;
  }
  
  QString Token::toQString()
  {
      QString tempstring;
      if (inttoken)
      {
          QString temp;
          temp.setNum(intpart);
          tempstring.append('[');
          tempstring.append(temp);
          tempstring.append(']');
          return tempstring;
      }
      else if (doubletoken)
      {
          QString temp;
          temp.setNum(doublepart);
          tempstring.append('[');
          tempstring.append(temp);
          tempstring.append(']');
          return tempstring;
      }
      else if (fractiontoken)
      {
          Fraction F = fractionpart;
          QString temp;
          tempstring.append('[');
          temp.setNum(F.Numerator());
          tempstring.append(temp);
          tempstring.append('/');
          temp.setNum(F.Denominator());
          tempstring.append(temp);
          tempstring.append(']');
          return tempstring;
      }
      else if (mixedtoken)
      {
          Mixed M = mixedpart;
          QString temp;
          tempstring.append('[');
          temp.setNum(M.WholePart());
          tempstring.append(temp);
          tempstring.append(' ');
          temp.setNum(M.Numerator());
          tempstring.append(temp);
          tempstring.append('/');
          temp.setNum(M.Denominator());
          tempstring.append(temp);
          tempstring.append(']');
          return tempstring;
      }
      else if (operatortoken)
      {
          tempstring.append('[');
          tempstring.append(operatorpart);
          tempstring.append(']');
      }
      else
          throw UNKNOWN_TOKEN;
  }
  
  void Token::Print(ostream&amp; out) const
  {
      if (inttoken)
      {
          out &lt;&lt; intpart;
      }
      else if (doubletoken)
      {
          out &lt;&lt; doublepart;
      }
      else if (fractiontoken)
      {
          out &lt;&lt; fractionpart;
      }
      else if (mixedtoken)
      {
          out &lt;&lt; mixedpart;
      }
      else if (operatortoken)
      {
          out &lt;&lt; operatorpart;
      }
      else
          throw 1;
  
  }
  
  ostream&amp; operator&lt;&lt;(ostream&amp; out, const Token&amp; T)
  {
      T.Print(out);
      return out;
  }
  
  bool Token::isLeftParen()
  {
      if (operatortoken &amp;&amp; operatorpart == '(')
          return 1;
      return 0;
  }
  
  bool Token::isRightParen()
  {
      if (operatortoken &amp;&amp; operatorpart == ')')
          return 1;
      return 0;
  }
  
  bool Token::isOperator()
  {
      if (operatortoken)
          return 1;
      return 0;
  }
  </code></pre>