[英]C++ Class Variable Scope
我對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))。 感謝那些回答。
double Drinks::input_money(int c)
// ^^^^^^^^ forgot this
您在提供實現時忘記了類名。
注意您的定義之間的區別
void Drinks::display_data
和
double input_money(int c)
在第二種情況下,您定義了一個自由函數,該函數不是該類的成員,並且沒有有關該類成員的信息。 它應該是
double Drinks::input_money(int c)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.