2017-02-24 8 views
-2

私はこのRPN電卓のプロジェクトを手に入れました。私はそのためのすべてのコードを投稿しようとします。私は "C++ dc.cpp"を実行しますか?他の誰かが正しくコンパイルしていることを知っていても、私はエラーを返します。これをどのようにしてコンパイルしますか?私は間違った方法でコンパイルしていますか?

dc.cpp

#include <iostream> 
#include "stack.h" 
#include <string> 
#include "dsexceptions.h" 

using namespace std; 

bool IsOperator(char op); 
int calculate(char op, int operand1, int operand2); 

int main() { 
    string input; 
    Stack<int> dc; 

    while (getline(cin, input)) 
    { 
     for (int i = 0; i<input.length(); i++) 
     { 
      if (input[i] == ' ') 
       continue; 
      else if (IsOperator(input[i])) 
      { 

       try { 
        int operand2 = dc.top(); 
        dc.pop(); 

        int operand1 = dc.top(); 
        dc.pop(); 
        int result = calculate(input[i], operand1, operand2); 
        dc.push(result); 

       } 
       catch (Underflow e) { 
        cout << "No elements in stack"; 

       } 
       catch (Overflow e) { 
        cout << "Stack full. Can't insert more"; 

       } 
       catch (DivisionByZero e) { 
        cout << "Please choose some other value for division except 0"; 

       } 
       catch (InvalidOperator e) { 
        cout << "The operator you choose is invalid"; 
       } 
      } 
      else if (isdigit(input[i])) 
      { 
       int operand = 0; 
       while (i<input.length() && isdigit(input[i])) 
       { 
        operand = (operand * 10) + (input[i] - '0'); 
        i++; 
       } 
       i--; 

       if (i && input[i - 1] == '_') 
        operand *= -1; 

       try { 
        dc.push(operand); 
       } 
       catch (Overflow e) { 
        cout << "Stack full. Can't insert more"; 
       } 

      } 
      else if (input[i] == 'p') { 
       try { 
        cout << dc.top() << endl; 
       } 
       catch (Underflow e) { 
        cout << "No elements in stack"; 
       } 

      } 
      else if (input[i] == 'n') { 
       try { 
        cout << dc.top(); 
       } 
       catch (Underflow e) { 
        cout << "No elements in stack"; 
       } 

      } 
      else if (input[i] == 'f') { 
       for (Stack <int> dump = dc; !dump.isEmpty(); dump.pop()) { 

        try { 
         cout << dump.top() << " "; 
        } 
        catch (Underflow e) { 
         cout << "No elements in stack"; 
        } 

       } 
       cout << endl; 
      } 
      else if (input[i] == 'c') { 
       while (!dc.isEmpty()) 
        dc.pop(); 
      } 
      else if (input[i] == 'd') { 

       try { 
        dc.push(dc.top()); 
       } 
       catch (Overflow e) { 
        cout << "Stack full. Can't insert more"; 
       } 
       catch (Underflow e) { 
        cout << "No elements in stack"; 
       } 


      } 
      else if (input[i] == 'r') { 

       try { 
        int x = dc.top(); 
        dc.pop(); 
        int y = dc.top(); 
        dc.pop(); 

        dc.push(x); 
        dc.push(y); 
       } 
       catch (Overflow e) { 
        cout << "Stack full. Can't insert more"; 
       } 
       catch (Underflow e) { 
        cout << "No elements in stack"; 
       } 
      } 

     } 
    } 

    return 0; 
} 

bool IsOperator(char op) 
{ 
    if (op == '+' || op == '-' || op == '*' || op == '/' || op == '%') 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

int calculate(char op, int operand1, int operand2) 
{ 
    if (op == '+') 
    { 
     return operand1 + operand2; 
    } 
    else if (op == '-') 
    { 
     return operand1 - operand2; 
    } 
    else if (op == '*') 
    { 
     return operand1 * operand2; 
    } 
    else if (op == '/') 
    { 
     return operand1/operand2; 
     if (operand2 == 0) 
     { 
      throw DivisionByZero(); 
     } 
    } 
    else if (op == '%') 
    { 
     return operand1 % operand2; 
    } 

    else { 
     throw InvalidOperator(); 
    } 
} 

dsexceptions.h

#ifndef _DSEXCEPTIONS_H_ 
#define _DSEXCEPTIONS_H_ 

#include <iostream> 


class Underflow : public std::runtime_error 
{ 
public: 
    Underflow(std::string const& error) 
     : std::runtime_error(error) {} 

    Underflow() 
     :std::runtime_error("Underflow Exception") {} 
}; 

class Overflow : public std::runtime_error 
{ 

public: 
    Overflow(std::string const& error) 
     : std::runtime_error(error) {} 

    Overflow() 
     :std::runtime_error("Overflow Exception") {} 
}; 

class InvalidOperator : public std::runtime_error 
{ 

public: 
    InvalidOperator(std::string const& error) 
     : std::runtime_error(error) {} 

    InvalidOperator() 
     :std::runtime_error("Invalid Exception") {} 
}; 



class OutOfMemory : public std::runtime_error 
{ 

public: 
    OutOfMemory(std::string const& error) 
     : std::runtime_error(error) {} 

    OutOfMemory() 
     :std::runtime_error("Out Of Memory Exception") {} 
}; 

class BadIterator : public std::runtime_error 
{ 

public: 
    BadIterator(std::string const& error) 
     : std::runtime_error(error) {} 

    BadIterator() 
     :std::runtime_error("Bad Iterator Exception") {} 
}; 

class DataError : public std::runtime_error 
{ 

public: 
    DataError(std::string const& error) 
     : std::runtime_error(error) {} 

    DataError() 
     :std::runtime_error("Data Error Exception") {} 
}; 

class DivisionByZero : public std::runtime_error 
{ 

public: 
    DivisionByZero(std::string const& error) 
     : std::runtime_error(error) {} 

    DivisionByZero() 
     :std::runtime_error("Division By Zero Exception") {} 
}; 

#endif 

stack.cpp

#include "dsexceptions.h" 
#include "stack.h" 
/** 
* Construct the stack. 
*/ 
template <class Object> 
Stack<Object>::Stack(int aCapacity) 
{ 
    topOfStack = -1; 
    capacity = aCapacity; 
    theArray.resize(aCapacity); 
} 

/** 
* Test if the stack is logically empty. 
* Return true if empty, false otherwise. 
*/ 
template <class Object> 
bool Stack<Object>::isEmpty() const 
{ 
    return topOfStack == -1; 
} 

/** 
* Test if the stack is logically full. 
* Return true if full, false otherwise. 
*/ 
template <class Object> 
bool Stack<Object>::isFull() const 
{ 
    return topOfStack == capacity - 1; 
} 

/** 
* Make the stack logically empty. 
*/ 
template <class Object> 
void Stack<Object>::makeEmpty() 
{ 
    topOfStack = -1; 
} 

/** 
* Get the most recently inserted item in the stack. 
* Does not alter the stack. 
* Return the most recently inserted item in the stack. 
* Exception Underflow if stack is already empty. 
*/ 
template <class Object> 
const Object & Stack<Object>::top() const 
{ 
    if (isEmpty()) 
     throw Underflow(); 
    return theArray[topOfStack]; 
} 

/** 
* Remove the most recently inserted item from the stack. 
* Exception Underflow if stack is already empty. 
*/ 
template <class Object> 
void Stack<Object>::pop() 
{ 
    if (isEmpty()) 
     throw Underflow(); 
    topOfStack--; 
} 

/** 
* Insert x into the stack, if not already full. 
* Exception Overflow if stack is already full. 
*/ 
template <class Object> 
void Stack<Object>::push(const Object & x) 
{ 
    if (isFull()) 
     throw Overflow(); 
    theArray[++topOfStack] = x; 
} 

/** 
* Return and remove most recently inserted item from the stack. 
* Return most recently inserted item. 
* Exception Underflow if stack is already empty. 
*/ 
template <class Object> 
Object Stack<Object>::topAndPop() 
{ 
    if (isEmpty()) 
     throw Underflow(); 
    return theArray[topOfStack--]; 
} 


/* int main() 
    { 
     cout << "testing stack class" << endl; 
     Stack<int> dc(100); 
     cout << " stack class init" << endl; 
     dc.push(10); 

     dc.pop(); 
     cout << "done testing stack class" << endl; 
    }*/ 

私のコンパイラは目を返します。私が "C++ dc.cpp"を実行すると、エラーが発生します。私はちょうど、間違ってそれをコンパイルする必要がある?:

In file included from stack.cpp:1: 
dsexceptions.h:8: error: expected class-name before ‘{’ token 
dsexceptions.h: In constructor ‘Underflow::Underflow(const std::string&)’: 
dsexceptions.h:11: error: expected class-name before ‘(’ token 
dsexceptions.h:11: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: In constructor ‘Underflow::Underflow()’: 
dsexceptions.h:14: error: expected class-name before ‘(’ token 
dsexceptions.h:14: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: At global scope: 
dsexceptions.h:18: error: expected class-name before ‘{’ token 
dsexceptions.h: In constructor ‘Overflow::Overflow(const std::string&)’: 
dsexceptions.h:22: error: expected class-name before ‘(’ token 
dsexceptions.h:22: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: In constructor ‘Overflow::Overflow()’: 
dsexceptions.h:25: error: expected class-name before ‘(’ token 
dsexceptions.h:25: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: At global scope: 
dsexceptions.h:29: error: expected class-name before ‘{’ token 
dsexceptions.h: In constructor ‘InvalidOperator::InvalidOperator(const std::string&)’: 
dsexceptions.h:33: error: expected class-name before ‘(’ token 
dsexceptions.h:33: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: In constructor ‘InvalidOperator::InvalidOperator()’: 
dsexceptions.h:36: error: expected class-name before ‘(’ token 
dsexceptions.h:36: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: At global scope: 
dsexceptions.h:42: error: expected class-name before ‘{’ token 
dsexceptions.h: In constructor ‘OutOfMemory::OutOfMemory(const std::string&)’: 
dsexceptions.h:46: error: expected class-name before ‘(’ token 
dsexceptions.h:46: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: In constructor ‘OutOfMemory::OutOfMemory()’: 
dsexceptions.h:49: error: expected class-name before ‘(’ token 
dsexceptions.h:49: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: At global scope: 
dsexceptions.h:53: error: expected class-name before ‘{’ token 
dsexceptions.h: In constructor ‘BadIterator::BadIterator(const std::string&)’: 
dsexceptions.h:57: error: expected class-name before ‘(’ token 
dsexceptions.h:57: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: In constructor ‘BadIterator::BadIterator()’: 
dsexceptions.h:60: error: expected class-name before ‘(’ token 
dsexceptions.h:60: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: At global scope: 
dsexceptions.h:64: error: expected class-name before ‘{’ token 
dsexceptions.h: In constructor ‘DataError::DataError(const std::string&)’: 
dsexceptions.h:68: error: expected class-name before ‘(’ token 
dsexceptions.h:68: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: In constructor ‘DataError::DataError()’: 
dsexceptions.h:71: error: expected class-name before ‘(’ token 
dsexceptions.h:71: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: At global scope: 
dsexceptions.h:75: error: expected class-name before ‘{’ token 
dsexceptions.h: In constructor ‘DivisionByZero::DivisionByZero(const std::string&)’: 
dsexceptions.h:79: error: expected class-name before ‘(’ token 
dsexceptions.h:79: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: In constructor ‘DivisionByZero::DivisionByZero()’: 
dsexceptions.h:82: error: expected class-name before ‘(’ token 
dsexceptions.h:82: error: expected ‘{’ before ‘(’ token 
In file included from stack.cpp:2: 
stack.h: At global scope: 
stack.h:1: error: expected constructor, destructor, or type conversion before ‘<’ token 
stack.h:34: error: ISO C++ forbids declaration of ‘vector’ with no type 
stack.h:34: error: expected ‘;’ before ‘<’ token 
stack.h:36: error: ISO C++ forbids declaration of ‘vector’ with no type 
stack.h:36: error: expected ‘;’ before ‘<’ token 
stack.cpp: In constructor ‘Stack<Object>::Stack(int)’: 
stack.cpp:11: error: ‘theArray’ was not declared in this scope 
stack.cpp: In member function ‘const Object& Stack<Object>::top() const’: 
stack.cpp:54: error: ‘theArray’ was not declared in this scope 
stack.cpp: In member function ‘void Stack<Object>::push(const Object&)’: 
stack.cpp:78: error: ‘theArray’ was not declared in this scope 
stack.cpp: In member function ‘Object Stack<Object>::topAndPop()’: 
stack.cpp:91: error: ‘theArray’ was not declared in this scope 
[[email protected] Prog2]$ c++ dc.cpp 
In file included from dc.cpp:2: 
stack.h:1: error: expected constructor, destructor, or type conversion before ‘<’ token 
stack.h:34: error: ISO C++ forbids declaration of ‘vector’ with no type 
stack.h:34: error: expected ‘;’ before ‘<’ token 
stack.h:36: error: ISO C++ forbids declaration of ‘vector’ with no type 
stack.h:36: error: expected ‘;’ before ‘<’ token 
In file included from dc.cpp:4: 
dsexceptions.h:8: error: expected class-name before ‘{’ token 
dsexceptions.h: In constructor ‘Underflow::Underflow(const std::string&)’: 
dsexceptions.h:11: error: expected class-name before ‘(’ token 
dsexceptions.h:11: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: In constructor ‘Underflow::Underflow()’: 
dsexceptions.h:14: error: expected class-name before ‘(’ token 
dsexceptions.h:14: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: At global scope: 
dsexceptions.h:18: error: expected class-name before ‘{’ token 
dsexceptions.h: In constructor ‘Overflow::Overflow(const std::string&)’: 
dsexceptions.h:22: error: expected class-name before ‘(’ token 
dsexceptions.h:22: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: In constructor ‘Overflow::Overflow()’: 
dsexceptions.h:25: error: expected class-name before ‘(’ token 
dsexceptions.h:25: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: At global scope: 
dsexceptions.h:29: error: expected class-name before ‘{’ token 
dsexceptions.h: In constructor ‘InvalidOperator::InvalidOperator(const std::string&)’: 
dsexceptions.h:33: error: expected class-name before ‘(’ token 
dsexceptions.h:33: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: In constructor ‘InvalidOperator::InvalidOperator()’: 
dsexceptions.h:36: error: expected class-name before ‘(’ token 
dsexceptions.h:36: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: At global scope: 
dsexceptions.h:42: error: expected class-name before ‘{’ token 
dsexceptions.h: In constructor ‘OutOfMemory::OutOfMemory(const std::string&)’: 
dsexceptions.h:46: error: expected class-name before ‘(’ token 
dsexceptions.h:46: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: In constructor ‘OutOfMemory::OutOfMemory()’: 
dsexceptions.h:49: error: expected class-name before ‘(’ token 
dsexceptions.h:49: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: At global scope: 
dsexceptions.h:53: error: expected class-name before ‘{’ token 
dsexceptions.h: In constructor ‘BadIterator::BadIterator(const std::string&)’: 
dsexceptions.h:57: error: expected class-name before ‘(’ token 
dsexceptions.h:57: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: In constructor ‘BadIterator::BadIterator()’: 
dsexceptions.h:60: error: expected class-name before ‘(’ token 
dsexceptions.h:60: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: At global scope: 
dsexceptions.h:64: error: expected class-name before ‘{’ token 
dsexceptions.h: In constructor ‘DataError::DataError(const std::string&)’: 
dsexceptions.h:68: error: expected class-name before ‘(’ token 
dsexceptions.h:68: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: In constructor ‘DataError::DataError()’: 
dsexceptions.h:71: error: expected class-name before ‘(’ token 
dsexceptions.h:71: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: At global scope: 
dsexceptions.h:75: error: expected class-name before ‘{’ token 
dsexceptions.h: In constructor ‘DivisionByZero::DivisionByZero(const std::string&)’: 
dsexceptions.h:79: error: expected class-name before ‘(’ token 
dsexceptions.h:79: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: In constructor ‘DivisionByZero::DivisionByZero()’: 
dsexceptions.h:82: error: expected class-name before ‘(’ token 
dsexceptions.h:82: error: expected ‘{’ before ‘(’ token 
[[email protected] Prog2]$ c++ dc.cpp 
In file included from dc.cpp:2: 
stack.h:1: error: expected constructor, destructor, or type conversion before ‘<’ token 
stack.h:34: error: ISO C++ forbids declaration of ‘vector’ with no type 
stack.h:34: error: expected ‘;’ before ‘<’ token 
stack.h:36: error: ISO C++ forbids declaration of ‘vector’ with no type 
stack.h:36: error: expected ‘;’ before ‘<’ token 
In file included from dc.cpp:4: 
dsexceptions.h:8: error: expected class-name before ‘{’ token 
dsexceptions.h: In constructor ‘Underflow::Underflow(const std::string&)’: 
dsexceptions.h:11: error: expected class-name before ‘(’ token 
dsexceptions.h:11: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: In constructor ‘Underflow::Underflow()’: 
dsexceptions.h:14: error: expected class-name before ‘(’ token 
dsexceptions.h:14: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: At global scope: 
dsexceptions.h:18: error: expected class-name before ‘{’ token 
dsexceptions.h: In constructor ‘Overflow::Overflow(const std::string&)’: 
dsexceptions.h:22: error: expected class-name before ‘(’ token 
dsexceptions.h:22: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: In constructor ‘Overflow::Overflow()’: 
dsexceptions.h:25: error: expected class-name before ‘(’ token 
dsexceptions.h:25: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: At global scope: 
dsexceptions.h:29: error: expected class-name before ‘{’ token 
dsexceptions.h: In constructor ‘InvalidOperator::InvalidOperator(const std::string&)’: 
dsexceptions.h:33: error: expected class-name before ‘(’ token 
dsexceptions.h:33: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: In constructor ‘InvalidOperator::InvalidOperator()’: 
dsexceptions.h:36: error: expected class-name before ‘(’ token 
dsexceptions.h:36: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: At global scope: 
dsexceptions.h:42: error: expected class-name before ‘{’ token 
dsexceptions.h: In constructor ‘OutOfMemory::OutOfMemory(const std::string&)’: 
dsexceptions.h:46: error: expected class-name before ‘(’ token 
dsexceptions.h:46: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: In constructor ‘OutOfMemory::OutOfMemory()’: 
dsexceptions.h:49: error: expected class-name before ‘(’ token 
dsexceptions.h:49: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: At global scope: 
dsexceptions.h:53: error: expected class-name before ‘{’ token 
dsexceptions.h: In constructor ‘BadIterator::BadIterator(const std::string&)’: 
dsexceptions.h:57: error: expected class-name before ‘(’ token 
dsexceptions.h:57: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: In constructor ‘BadIterator::BadIterator()’: 
dsexceptions.h:60: error: expected class-name before ‘(’ token 
dsexceptions.h:60: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: At global scope: 
dsexceptions.h:64: error: expected class-name before ‘{’ token 
dsexceptions.h: In constructor ‘DataError::DataError(const std::string&)’: 
dsexceptions.h:68: error: expected class-name before ‘(’ token 
dsexceptions.h:68: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: In constructor ‘DataError::DataError()’: 
dsexceptions.h:71: error: expected class-name before ‘(’ token 
dsexceptions.h:71: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: At global scope: 
dsexceptions.h:75: error: expected class-name before ‘{’ token 
dsexceptions.h: In constructor ‘DivisionByZero::DivisionByZero(const std::string&)’: 
dsexceptions.h:79: error: expected class-name before ‘(’ token 
dsexceptions.h:79: error: expected ‘{’ before ‘(’ token 
dsexceptions.h: In constructor ‘DivisionByZero::DivisionByZero()’: 
dsexceptions.h:82: error: expected class-name before ‘(’ token 
dsexceptions.h:82: error: expected ‘{’ before ‘(’ token 
+0

エラー処理を実装する前に、すべてのことがうまくいっていることにも言及してください。 – knm

+0

stack.hファイルを投稿できますか?私はそこにセミコロンがないかもしれないと思います。 – ltjax

+0

オペレーティングシステム、コンパイラ、およびどのバージョンを使用していますか?どのようにしてコードをコンパイルしたのですか?(最近の* [GCC](http://gcc.gnu.org/)の 'g ++ -Wall -g'をお勧めします) –

答えて

1

あなたは罰金それをコンパイルしているように見えますが、あなたのコードは、コンパイラがについてあなたを語っているエラーを持っています。非常に少なくとも

、追加...

#include <stdexcept> 

...あなたのdsexceptions.hファイルへ。

また、あなたのstack.hファイルに... ...

#include <vector> 

を追加する必要があります。

+0

これは答えではありません。 okのコメントですが、okの答えはありません。 –

+1

私は壁の壁のエラーと思う、それは合理的な答えです。明らかに、問題は、OPが適切なヘッダーを含んでいないことです。 – MuertoExcobito

0

エラーメッセージをより注意深く読む必要があります。ちょうど最初のメッセージを撮影:あなたはコンパイラがすぐ{の前にトークンを認識しないことがわかります

dsexceptions.h:8: error: expected class-name before ‘{’ token 

。そのトークンは何ですか? 7行目は、はい、あなたはstd::runtime_errorを使用している

class Underflow : public std::runtime_error 

ですが、それを定義していません。修正はもちろん、#include <iostream>の代わりに#include <stdexcept>になります。

ヘッダーには、通常、ヘッダーに必要な定義(ヘッダーに必要なもののみ)を含める必要があります。そうしないと、ヘッダを使用しているコードが前提条件を知らない場合、ヘッダーが上記のように失敗する非常に壊れやすいコードに終わる可能性が高くなります。標準のライブラリヘッダーを複数回インクルードしても問題はありません。うまく書かれたライブラリヘッダーについても同様です。

ソースファイルの不足または不必要な内容を分析するのに役立つinclude-what-you-use(別名iwyu)などのツールがあります。それが役立つかどうかは確かに分かります。

関連する問題