C ++类变量范围

时间:2012-02-27 20:11:01

标签: c++ arrays class scope structure

我对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函数无法访问数组中的结构。

谢谢!

编辑:总的noob错误/粗心。忘记在input_money函数定义中添加类的名称并使用范围解析运算符(即应该是Drinks :: input_money(int c))。感谢那些回答。

2 个答案:

答案 0 :(得分:5)

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

在提供实施时忘记了班级名称。

答案 1 :(得分:1)

请注意

的定义之间的区别

void Drinks::display_data

double input_money(int c)

在第二种情况下,您定义了一个自由函数,该函数不是该类的成员,并且没有关于类成员的信息。它应该是

double Drinks::input_money(int c)