Thread: Compile errors with overloading >> operators

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    7

    Compile errors with overloading >> operators

    I do not understand why my compiler is saying:
    type specifier omitted for parameter

    FULL COMPILE ERRORS:
    Terms.cpp:12: type specifier omitted for parameter
    Terms.cpp:12: parse error before `&'
    Terms.cpp:13: `operator >>(...)' must have an argument of class or enumerated type
    Terms.cpp:13: `operator >>(...)' must take exactly two arguments
    Terms.cpp: In function `class istream & operator >>(...)':
    Terms.cpp:32: `istr' undeclared (first use this function)
    Terms.cpp:32: (Each undeclared identifier is reported only once
    Terms.cpp:32: for each function it appears in.)
    Terms.cpp:41: `data' undeclared (first use this function)
    Terms.cpp:62: `ture' undeclared (first use this function)

    I tried adding:
    istream:: istream& operator >> (istream& istr, Term& data)
    but that did not work aswell


    My .h file includes:
    Code:
    #ifndef TERMS_H
    #define TERMS_H
    
    #include <iostream>
    
    class Terms
    {
      public:
              Terms() {};
              // Default constructor
    
              void SetCoefficient (int a) { coef = a; };
              // Sets coef
              // Pre:  An object Term has been declared
              // Post: coef is set to the number passed in
    
              void SetExponent (int a) { exp = a; };
              // Sets exp
              // Pre:  An object Term has been declared
              // Post: coef is set to the number passed in
    
              void SetTermPrint (bool a) { okToPrint = a; };
              // If the string being proccess contains ONLY a + or a -
              // This will be set to false since we do not want to store
              // these items in the linked list with an array of records
              // Pre:  An object Term has been declared
              // Post: If the term is only a + or a - okToPrint is false
              //       else true.
    
              void SetIfAnX (bool a) { hasAnX = a; };
              // If the term being proccessed contains an X we set a to true
              // Else false. This is neccessary since when we eventually find
              // the polynomials derivative if no exists exist we want to ignore
              // this term since it becomes 0.
              // Pre:  An object Term has been declared
              // Post: If the string being passed in contains an x this sets hasAnX to be true
              //       else false;
    
              int GetCoefficient () { return coef; };
              // Returns what is stored in coef
              // Post: coef is returned
    
              int GetExponent () { return exp; };
              // Returns what is stored in exp
              // Post: exp is returned
    
              bool GetTermPrint() { return okToPrint; };
              // Returns what is stored in okToPrint;
              // Post: okToPrint is reutrned
    
              bool GetIfAnX () { return hasAnX; };
              // Returns what is stored in hasAnX
              // Post: hasAnX is returned
    
      private:
              int coef;
              int exp;
              bool okToPrint;
              bool hasAnX;
    //        friend istream& operator >> ( istream&, Terms& );
    };
    
    istream& operator >> (istream& istr, Terms& data);
    
    #endif
    and my .cpp includes
    Code:
    // Terms.cpp
    // This class is used to overload the input operator and set an object Term to have:
    // 1) its coefficient
    // 2) its exponent
    // 3) if it contains an x
    // 4) if it is needed to print
    
    #include <stdlib.h>
    #include <string>
    #include "Terms.h"
    
    istream& operator >> (istream& istr, Term& data)
    {
      int number;
      int exponent;
      bool print;
      bool isThere;
      bool isNegative;
      string line;
      string tempNum;
      string tempExp;
      const char* num;
      const char* exp;
      int length;
    
      isNegative = false;
      print = true;
      isThere = false;
      tempNum = "";
      tempExp = "";
    
      istr >> line;
      cout << "THE LINE: " << line << endl;
    
      if ( !istr.fail() )
      {
        if (line == "-")
        {
          isNegative = true;
          print = false;
          data.SetPrint(print);
        }
        else if (line == "+")
        {
          print = false;
          data.SetPrint(print);
        }
        else
        {
          line = line + ";";
    
          length = line.size();
    
          for (int i = 0; i < length; i++)
          {
            if( isdigit(line[i]) )
            {
              tempNum = tempNum + line[i];
            }
            else if ( line[i] == 'x' )
            {
              isThere = ture;
              i++;
              if ( line[i] == ';' )
                tempExp = tempExp + "1";
              i--;
              if (tempNum == "" || tempNum == "-")
                tempNum = tempNum + "1";
            }
            else if( line[i] == '^' )
            {
              i++;
              while ( line[i] != ';')
              {
                tempExp = tempExp + line[i];
                i++;
              }
            }
            else if ( line[i] = '-' )
            {
              tempNum = tempNum + line[i];
            }
          }
    
          if ( tempExp == "" )
            tempExp = "1";
    
          num = tempNum.c_str();
          exp = tempExp.c_str();
    
          number = atoi(num);
          exponent = atoi(exp);
    
          if (isNegative)
          {
            number *= -1;
            isNegative = false;
          }
        }
      }
    
      data.SetPrint(print);
      data.SetIfAnX(isThere);
      data.SetNumber(number);
      data.SetCoefficient(exponent);
    
    
      return istr;
    }
    Thanks,
    Chris

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    7
    never mind guys I figured this out

    For those of you who are interested what I did was I changed the following

    the includes in my .h file is now:
    Code:
    #include <iostream>
    and my declaration of the function in the .h is now:
    Code:
    friend istream& operator>> ( istream&, Terms& )
    then in my .cpp I changed the includes to:
    Code:
    #include <cstdlib>
    #include <string>
    #include "Terms.h"
    then my function call in .cpp to:
    Code:
    istream& operator >> (istream& istr, Terms& data)
    I have no clue what this works... I copied the original directly from a textbook. However our school computers do run of linux. Is it an error with different ways of doing stuff from windows and linux or something else?

    I am curious if anyone would know why this worked...

    Thanks,
    Chris

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading the << and >> operators
    By WebmasterMattD in forum C++ Programming
    Replies: 9
    Last Post: 10-15-2002, 05:22 PM
  2. Quake 2 Compile Errors
    By Mercury in forum Game Programming
    Replies: 9
    Last Post: 01-26-2002, 09:40 PM
  3. Two compile errors i need help with.
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 01-06-2002, 09:52 AM
  4. Compile Errors ...
    By ginoitalo in forum C++ Programming
    Replies: 1
    Last Post: 12-31-2001, 02:22 AM
  5. Compile Errors Due to STL & Iterator
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-02-2001, 10:08 AM