2012-02-15 10 views
-1

このプログラムで作業していますが、2エラーが表示されています。私は、プログラムが+ BCとして任意の式を読み取ったりする必要があり ....それらを修正(A + B)-c ...など と括弧の一致またはここにいない コードエラーC2664: 'Stack <type> :: equal':パラメータ1を 'char'から 'Stack'に変換できません。& '

// A simple Integer stack, Array based 
#include <string> 
#include <iostream> 
#define STACK_MAX 100 
#define equation 

using namespace std; 
template <typename type> 
class Stack { 

private: 
    type  data[STACK_MAX]; 
    int  size; 

public: 
    Stack(); 
    ~Stack() { } // Destructor 
    int top(); 
    void push(int d) ; 
    void pop() ; 
    int is_full(); 
    int is_empty() ; 
    void equal(Stack& entry) ; 
}; 
template <typename type > int Stack<type>::is_full() { 
    return (STACK_MAX >= size); 
} 
template <typename type > int Stack<type>::is_empty() { 
    return (STACK_MAX == 0); 
} 
template <typename type> Stack <type>::Stack() { 
    size = 0; 
} 
template <typename type > int Stack<type>:: top() { 

    if (is_empty()) { 
     printf("Error: stack empty\n"); 
     return -1; 
    } 
    return data[size-1]; 
} 
template <typename type > void Stack <type>::push (int d) { 

    if (! is_full()) { 
     data[size++] = d; 
    } else { 
     printf("Error: stack full\n"); 
    } 
} 
template < typename type > void Stack <type>::pop() { 

    if (size == 0) { 
     printf("Error: stack empty\n"); 
    } else { 
     size--; 
    } 
} 
template <typename type > void Stack <type> ::equal (Stack& entry) { 
    std::string& equation ; 
    char left = '(' ; 
    char right= ')' ; 
    Stack <type> x ; 
    string::size_type a ; 
    char next ; 
    next = entry ; 
    bool failed = false ; 
    for (a= 0 ; ! equation.length() ; ++a) { 
     next = equation[a] ; 
     if (next == left) { 
      x.push (next); 
     } else { 
      if (x.is_empty()) { 
       x.pop() ; 
      } else { 
       failed = true ; 
      } 
     } 

    } 
    if (a == 0) { 
     cout << " No parenthesis"<< endl ; 
    } else if (x.top() == left) { 
     cout << "Parenthesis don’t match. Missing right parenthesis" << endl ; 
    } else if (x.top() == right) { 
     cout << "Parenthesis don’t match. Missing left parenthesis " << endl ; 
    } else { 
     cout << "Matching parenthesis" << endl ; 
    } 

} 


int main() { 

    Stack <char> equation2 ; 
    char b ; 
    cout << " please enter your equation " << endl ; 
    for (int i =0 ; i< 100 ; i++) { 

     cin >> b ; 
     equation2.equal(b) ; 
    } 


    system ("pause") ; 


    return 0; 
} 
であるかどうかを確認

ここにエラーがあります。

2 IntelliSense: a reference of type "Stack<char> &" (not const-qualified) cannot be initialized with a value of type "char" c:\users\mike\documents\visual studio 2010\projects\stack\stack\stack.cpp 101 
    Error 1 error C2664: 'Stack<type>::equal' : cannot convert parameter 1 from 'char' to 'Stack<type> &' c:\users\mike\documents\visual studio 2010\projects\stack\stack\stack.cpp 101 
+0

べきではないisFullリターン(STACK_MAX <=サイズ)をcharに変更する必要がありますか? –

+1

これは間違っているようです。スタックに何かを押し込むことは決してありません。しかしあなたはそれを '比較'したいと思う。スタックと文字を比較することができます。とにかく、スタックは 'ints'を保持することになっているので、本当に' chars'を読みたいとは思っていません。 **あなたは何を達成したいですか?** – sehe

答えて

0

以前の回答は何らかの理由で削除されたので、私は代わりに投稿すると思います。

あなたが言ったように、equalの現在の実装は、動作する型がchar型であることに依存します。タイプを使用してテンプレート化されたクラスをインスタンス化した場合でも期待どおりに機能するだけなので、型名は& char。それでも、現在の問題は、同じ署名が間違っているスタック&を取ることで、あなたは

template <typename type > void Stack <type> ::equal (Stack& entry) 

template <typename type > void Stack <type> ::equal (char& entry) 
関連する問題