nik*_*nik 0 c++ arrays scope structure class
我对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
Run Code Online (Sandbox Code Playgroud)
这是实现文件:
/* 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";
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感激!我似乎无法弄清楚为什么input_money函数无法访问数组中的结构.
谢谢!
编辑:总的noob错误/粗心.忘记在input_money函数定义中添加类的名称并使用范围解析运算符(即应该是Drinks :: input_money(int c)).感谢那些回答的人.
double Drinks::input_money(int c)
// ^^^^^^^^ forgot this
Run Code Online (Sandbox Code Playgroud)
在提供实现时您忘记了类名.
| 归档时间: |
|
| 查看次数: |
829 次 |
| 最近记录: |