2015-01-11 133 views
0

我必須使用構造函數和析構函數來製作計算器,每次都會添加,減去,乘以和除去並返回總計。出於某種原因,當我將行「Calculator.add(num);」或任何「計算器」。部分返回給我一個錯誤,說它「期望一個標識符」。我錯過了一些簡單的東西嗎C++幫助中的計算器。無法返回運行總計

謝謝。 這是我的main.cpp文件。

#include <iostream> 
#include "Calculator.h" 
#include <cstdlib> 

using namespace std; 
double total; 
int main(){ 
    while (true){ 
    cout << "*** Calculator *** " << endl; 
    cout << "A: Add a value " << endl; 
    cout << "S: Subtract a value " << endl; 
    cout << "M: Multiply by a value " << endl; 
    cout << "D: Divide by a value " << endl; 
    cout << "T: Get the total " << endl; 
    cout << "Q: Quit " << endl; 
    cout << endl; 
    char input; 
    cin >> input; 
    if (input == 'A'){ 
     cout << "Current Total: " << total << endl; 
     cout << "Selection: A"; 
     cout << endl; 
     cout << "*** Add selected *** " << endl; 
     cout << "Value:"; 
     double num; 
     cin >> num; 
     cout << endl; 
     double turnTotal = total; 
     Calculator.add(num); 
     cout << turnTotal << "+" << num << " = " << total; 
    } 
    if (input == 'S'){ 
     cout << "Current Total: " << total << endl; 
     cout << "Selection: S"; 
     cout << endl; 
     cout << "*** Subtract selected *** " << endl; 
     cout << "Value: "; 
     double num2; 
     cin >> num2; 
     cout << endl; 
     double turnTotal2 = total; 
     Calculator.subtract(num2); 
     cout << turnTotal2 << "-" << num2 << "=" << total; 
    } 
    if (input == 'M'){ 
     cout << "Current Total: " << total << endl; 
     cout << "Selection: M"; 
     cout << endl; 
     cout << "*** Multiply selected *** " << endl; 
     cout << "Value: "; 
     double num3; 
     cin >> num3; 
     cout << endl; 
     double turnTotal3 = total; 
     Calculator.multiply(num3); 
     cout << turnTotal3 << "*" << num3 << "=" << total; 
    } 

    if (input == 'D'){ 
     cout << "Current Total: " << total << endl; 
     cout << "Selection: D"; 
     cout << endl; 
     cout << "*** Divide selected *** " << endl; 
     cout << "Value: "; 
     double num4; 
     cin >> num4; 
     cout << endl; 
     double turnTotal4 = total; 
     Calculator.divide(num4); 
     cout << turnTotal4 << "/" << num4 << "=" << total; 
    } 
    if (input == 'T'){ 
     cout << "Current Total: " << total << endl; 
     cout << "Selection: T"; 
     cout << endl; 
     cout << "*** Total selected *** " << endl; 
     cout << "Value: "; 
     double num5; 
     cin >> num5; 
     cout << endl; 
     double turnTotal5 = total; 
     Calculator.getTotal(num5); 
     cout << turnTotal5 << "-" << num5 << "=" << total; 
    } 
    if (input == 'Q'){ 
     cout << "Thank you for using the calculator! Bye bye! Have a great day!" << endl; 

    } 
    } 
} 

這裏是.cpp文件

#include <cstdlib> 
#include <iostream> 
#include "Calculator.h" 
using namespace std; 
Calculator::Calculator(double x){ 
    double total = x; 
    return; 
} 
double Calculator::getTotal(){ 
    return total; 
} 
void Calculator::add(double x){ 
    total += x; 
} 
void Calculator::subtract(double x){ 
    total -= x; 
} 
void Calculator::multiply(double x){ 
    total *= x; 
} 
void Calculator::divide(double x){ 
    total /= x; 
} 

而這裏的類.h文件中。

#include <cstdlib> 
#include <iostream> 
using namespace std; 

//class specification 
class Calculator { 

    public: 
    //constructor 
    Calculator(){double total = 0;} 
    Calculator(double total); 

    //member functions 
    void add(double x); 
    void subtract(double x); 
    void multiply(double x); 
    void divide(double x); 
    double getTotal(); 

    //destructor 
    ~Calculator(); 

    private: 
    //data 
    double total = 0; 
}; 
+0

'Calculator'是一個類定義,您需要實例的實例它能夠使用它。 –

+0

也許你應該先學習課程 - http://www.cplusplus.com/doc/tutorial/classes/ –

+0

'double total = x;'那不會做你想做的!提示:total是構造函數的局部變量。 – drescherjm

回答

4

你似乎並不第一初始化計算器。你可以對主要功能開始添加的初始化,舉例如下

double total; 
Calculator calc; 
int main(){ 
    while (true){ 

,然後用它像

calc.add(num); 
+0

感謝@JonathanPotter的編輯,看起來我的C++技能在做了這麼多JavaScript之後有點生疏...... :) –