Thread: infix to postfix using stack Conversion

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    64

    infix to postfix using stack Conversion

    I am Getting following errors I don't know why.I have mentioned the lines (in comments) where these errors are occurring.
    __________________________________________________
    warning C4018: '<' : signed/unsigned mismatch
    error C2064: term does not evaluate to a function taking 0 arguments
    error C2064: term does not evaluate to a function taking 0 arguments
    __________________________________________________ _

    Code:
    /////////////////////////////////////////////////
                  //libraries
    /////////////////////////////////////////////////
    #include <iostream>
    #include <string>
    using namespace std;
    /////////////////////////////////////////////////
               // General Stack
    ////////////////////////////////////////////////
    template <class Type>
    struct link{
        Type data;
        link<type>* next;
    };
    template <class Type>
    class stack{
    public:
        int size;
    link<Type>* top;
    
        stack(){
            size=0;
            top = NULL;
        }
        void additem(Type d);
        
        string pop();
    int get_size(){return size;}
    void push(string o);
    void stack_upper();
    bool stack_empty();
    void display();
    string top();
    
    };
    //////////////////////////////////////////////
                     //Stack Functions
    /////////////////////////////////////////////
    template<class Type>
    void stack<Type>::additem(Type d){
        link<Type>* newlink = new link<Type>;
        newlink->data=d;
        newlink->next=first;
        first=newlink;
    }
    
    template<class Type>
    void stack<Type>::display(){
        link<Type>* current=first;
        while(current!=NULL){
            cout << current->data << endl;
            current= current->next;
        }
    }
    template<class Type>
    string stack<Type>::top(){
        return top;
    }
    template<class Type>
    void stack<Type>::push(string ele)
    {
       link *temp; 
       temp =new link;
        if(top==NULL){
            temp->next =NULL;
        }
        else{
            temp->next =top;
        }
        temp->data=ele;
        top=temp;
        size++;
    }
    
    
    template<class Type>
    bool stack<Type>::stack_empty()
    {
        if(get_size()==0)
            return true;
        
        return false;
    }
    template<class Type>
    string stack<Type>::pop()
    {    string d;
        if(stack_empty()){
            cout<<"\nStack is Empty\n";      
            return ;
        }
        else{
          link *temp = top;
          top=top->next;
            d=temp->data;
          delete temp;
         size--;
        }
        return d;
    }
    //////////////////////////////////
           //Some Definitions
    /////////////////////////////////
    string infix;  // infix exression string
    string operand;
    string operate;
    stack <string> mystack; // this stack is used to push the operator
    string postfix;  // postfix string where the operand is appended
    ////////////////////////////////////////////////
             /////Expression Evaluator Class
    ////////////////////////////////////////////////
    class ee{
    private:
        
    public:
    string convert();
    bool isopp(char c);
    int precedence(string e);
    };
    //////////////////////////////////////////////////
         ///Expression Evaluator Class Functions
    /////////////////////////////////////////////////
    int ee::precedence(string e)
    {
    int f;
    if(e == "*" || e== "/" || e =="%")
     f = 2;
    else
    {
    if(e == "+" || e == "-")
     f = 1;
    }
    
    if(e=="."){
    f=0;
    }
    
    return f;
    
    }
    ee objee;
    string ee::convert()
    { 
     for(int i=0; i<infix.length(); i++)//warning C4018: '<' : signed/unsigned mismatch
     { 
    
     switch(infix[i]){
    
     // operate case start  
     case '+': case'-': case'*': case'/': case'^': case '(': case ')': case'.':
    
     operate=infix[i];
     {
      if(mystack.stack_empty() || objee.precedence(operate)>= objee.precedence(mystack.top())) // error C2064: term does not evaluate to a function taking 0 arguments
      {
       mystack.push(operate);    
       break;
      }       
    
      else 
      {
       while(!mystack.stack_empty() && objee.precedence(operate)< objee.precedence(mystack.top())) // error C2064: term does not evaluate to a function taking 0 arguments
       {
        postfix.append(mystack.top());
        mystack.pop();
       }
    
       mystack.push(operate); 
    
    
      }
     }//operate case closed
    
     default:        //when operand string char is parsed
      {
                            operand=infix[i];
                            postfix.append(operand);
                            break;
    
      } // default case closed
    
     }//switch closed
    
     }// for loop close
    
     while(!mystack.stack_empty())
     {
      postfix.append(mystack.top());
      mystack.pop();
     }
    
    return postfix;
    cout<<postfix;
    
    } // function close braces
    bool ee::isopp(char c){
        if (c=='+' || c=='-' || c=='*' || c=='/' || c=='%' || c==')' || c=='(' || c=='^')
        return true;
    else
        return  false;
    }
    
    
    string input()
    {
     cout<<" Enter the damn infix expression: "<<endl; 
     getline(cin, infix);
     return infix;
    }
    
    
    
    
    int main()
    {
    
    input();
    
    objee.convert();
    cout<<"postfix is "<<postfix<<endl;
    }

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Compiling that code with visual studio 2010 got me these errors:
    Code:
    main.cpp(13) : error C2065: 'type' : undeclared identifier
            main.cpp(14) : see reference to class template instantiation 'link<Type>' being compiled
    main.cpp(33) : error C2365: 'stack<Type>::top' : redefinition; previous definition was 'data member'
            main.cpp(19) : see declaration of 'stack<Type>::top'
            main.cpp(35) : see reference to class template instantiation 'stack<Type>' being compiled
    main.cpp(58) : error C2063: 'stack<Type>::top' : not a function
    main.cpp(58) : error C2350: 'stack<Type>::top' is not a static member
    main.cpp(33) : error C2365: 'stack<Type>::top' : redefinition; previous definition was 'data member'
            with
            [
                Type=std::string
            ]
            main.cpp(19) : see declaration of 'stack<Type>::top'
            with
            [
                Type=std::string
            ]
            main.cpp(106) : see reference to class template instantiation 'stack<Type>' being compiled
            with
            [
                Type=std::string
            ]
    main.cpp(143) : warning C4018: '<' : signed/unsigned mismatch
    main.cpp(153) : error C2064: term does not evaluate to a function taking 0 arguments
    main.cpp(161) : error C2064: term does not evaluate to a function taking 0 arguments
    main.cpp(161) : fatal error C1903: unable to recover from previous error(s); stopping compilation
    Now the first error is probably a typo. The second error in that list should serve you a good hint: you have a class member variable called top, and you have a method called top. That means you have two definitions for top.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. infix to postfix
    By Richieh in forum C++ Programming
    Replies: 2
    Last Post: 04-25-2012, 10:08 PM
  2. Infix to postfix stack class problem
    By stefanyco in forum C++ Programming
    Replies: 5
    Last Post: 10-05-2011, 01:04 AM
  3. Replies: 0
    Last Post: 05-12-2010, 01:30 AM
  4. Infix, Postfix, Pseudo-Calculator using Stack ADT
    By sangken in forum C Programming
    Replies: 9
    Last Post: 09-08-2006, 08:17 AM
  5. Replies: 4
    Last Post: 03-12-2006, 02:17 PM