2012-02-27 17 views
0

C++クラスの変数スコープに関する質問があります。私が取り組んでいる問題は、構造体の配列を保持するクラスを作成することです。各構造体には、特定のタイプの飲み物の名前、コスト、金額が入っています。C++クラス変数スコープ

クラスには、ドリンクを購入してメニューを表示するためのパブリックメンバー関数、および金銭入力(buy_drinkによって呼び出される)の取得と検証、および終点レポートの表示(デストラクタによって呼び出される)

プライベート関数input_moneyのスコープに問題があります。配列がまだ定義されていないというエラーが表示されます。私はdisplay_data関数(メニューの印刷用)をテストしましたが、それ自体は問題なく動作しましたが、input_moneyにスコープエラーがあり、display_dataにはない理由がわかりません。ここではヘッダファイルには、次のとおりです。

/* need to create a class that holds an array of 
    5 structures, each structure holding string drink name, 
    double cost, and int number in machine 

    class needs public functions to display data and 
    buy drink 

    private functions input money -- called by buy_drink to accept, 
    validate, and return to buy drink the amount of money input 

    daily report -- destructor that reports how much money 
    was made daily and how many pops are left in machine */ 

#ifndef DRINKS_H 
#define DRINKS_H 
#include <string> 

class Drinks 
{ 
private: 
    struct Menu 
    {         
    std::string name;     
    double cost; 
    int number; 
    }; 
    Menu list[5];    // array of 5 menu structures 
    double money_made;  // track money made during the day 
    double input_money(int); // return validated money to buy_drink() 
    void daily_report();  // called by deconstructor 

public: 
    Drinks();    
    ~Drinks();    
    void display_data(); 
    void buy_drink(int); 
}; 
#endif 

そしてここでは、実装ファイルです:

/* implementation file for Drinks class */ 

#include <iostream> 
#include <string> 
#include "drinks.h" 
using namespace std; 

const int SIZE = 5; 
const int START_SIZE = 100; 

Drinks::Drinks() 
{ 
    list[0].name = "Coke"; 
    list[1].name = "Root Beer"; 
    list[2].name = "Orange Soda"; 
    list[3].name = "Grape Soda"; 
    list[4].name = "Bottled Water"; 

    for (int count = 0; count < (SIZE-1); count++) 
    list[count].cost = .75; 
    list[4].cost = 1; 

    for (int count = 0; count < SIZE; count++) 
    list[count].number = 20; 

    money_made = 0; 
} 

void Drinks::display_data() 
{ 
    for (int count = 0; count < SIZE; count++) { 
    if (count == 0) 
     cout << count+1 << list[count].name << "\t\t$ "; 
    else 
     cout << count+1 << list[count].name << "\t$ "; 
    cout << list[count].cost << "\t" 
    << list[count].number << endl; 
    } 
} 

double input_money(int c) 
{ 
    double input; 

    cin >> input; 

    while (input != list[c].cost) { 
    if (input < list[c].cost) { 
     cout << "Not enough money.\n" 
     << "Enter " << list[c].cost - input 
     << " more cents to buy\n\n> "; 
     cin >> input; 
    } 
    else if (input > list[c].cost) { 
     cout << "Too much money.\n" 
     << "I only need $" << list[c].cost << endl 
     << "Enter " << input - list[c].cost 
     << " less money: "; 
     cin >> input; 
    } 
    } 

    return input; 
} 

void Drinks::buy_drink(int c)    // this receives an int choice (to access corresponding structure in the list array) 
{ 
    double input;        
    cout << "Enter " <<list[c].cost    
     << " to purchase " << list[c].name  
     << "\n\n> ";       
    input = input_money(c);     // input money returns a validated and accurate price for the drink and is passed the choice to access array 

    list[c].number -= 1; 
    money_made += list[c].cost;    // add cost of drink to money made 
} 

void Drinks::daily_report() 
{ 
    int end_size = 0; 

    for (int count = 0; count < SIZE; count++) 
    end_size += list[count].number; 

    cout << "Today, you made $" << money_made << endl; 
    cout << "There are " << START_SIZE - end_size 
     << " drinks left in the machine" << endl; 
} 

Drinks::~Drinks() 
{ 
    daily_report(); 

    cout << "goodbye mr anderson\n"; 
} 

すべてのヘルプははるかに高く評価されるだろう!私はinput_money関数が配列内の構造体にアクセスできない理由を理解できないようです。

ありがとうございました!

編集:合計の間違い/不注意。 input_money関数定義にクラスの名前を追加するのを忘れて、スコープ解決演算子を使用します(つまり、Drinks :: input_money(int c)にする必要があります)。答えた人に感謝します。

+1

でなければなりません。 –

答えて

5
double Drinks::input_money(int c) 
    // ^^^^^^^^ forgot this 

実装を提供しているときにクラス名を忘れてしまった。

+0

私はそれほど頻繁にそれを行うことは面白くない。 – chris

+0

ハ。 Woooooow、私のところでは非常に多くの失敗。それはあなたが休憩を取っていないときに起こることです。それを見ていただきありがとうございます。 – nik

1

お知らせ第2のケースで

void Drinks::display_data

double input_money(int c)

のあなたの定義の違いは、クラスのメンバーではなく、持っている無料の機能を定義していますクラスメンバーに関する情報はありません。それは、 `ダブルドリンク:: input_money(int型c)の代わりに` `ダブルinput_money(int型C)`のあるべき

double Drinks::input_money(int c)

+0

ハ。ありがとう。私はそれを残して信じられない。 – nik