我的程序中出现此错误
1>c:\users\kanaan\documents\visual studio 2010\projects\assign2\assign2\vending machine.h(36): error C2057: expected constant expression
1>c:\users\kanaan\documents\visual studio 2010\projects\assign2\assign2\vending machine.h(36): warning C4200: nonstandard extension used : zero-sized array in struct/union
1> Cannot generate copy-ctor or copy-assignment operator when UDT contains a zero-sized array
1> MakeSelection.cpp
1>c:\users\kanaan\documents\visual studio 2010\projects\assign2\assign2\vending machine.h(36): error C2057: expected constant expression
1>c:\users\kanaan\documents\visual studio 2010\projects\assign2\assign2\vending machine.h(36): warning C4200: nonstandard extension used : zero-sized array in struct/union
1> Cannot generate copy-ctor or copy-assignment operator when UDT contains a zero-sized array
1>c:\users\kanaan\documents\visual studio 2010\projects\assign2\assign2\makeselection.cpp(13): error C2082: redefinition of formal parameter 'ItemPrice'
1>c:\users\kanaan\documents\visual studio 2010\projects\assign2\assign2\makeselection.cpp(14): error C2082: redefinition of formal parameter 'NumItems'
1> Main.cpp
1>c:\users\kanaan\documents\visual studio 2010\projects\assign2\assign2\vending machine.h(36): error C2057: expected constant expression
1>c:\users\kanaan\documents\visual studio 2010\projects\assign2\assign2\vending machine.h(36): warning C4200: nonstandard extension used : zero-sized array in struct/union
1> Cannot generate copy-ctor or copy-assignment operator when UDT contains a zero-sized array
1> DisplayErrorMessage.cpp
1>c:\users\kanaan\documents\visual studio 2010\projects\assign2\assign2\vending machine.h(36): error C2057: expected constant expression
1>c:\users\kanaan\documents\visual studio 2010\projects\assign2\assign2\vending machine.h(36): warning C4200: nonstandard extension used : zero-sized array in struct/union
1> Cannot generate copy-ctor or copy-assignment operator when UDT contains a zero-sized array
需要帮忙。
这是一个完整的编码。
头文件:
#ifndef _VENDING_MACHINE_H_
#define _VENDING_MACHINE_H_
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
extern int Denominations;
extern int const ITEMS;
extern int Coins[];
extern int NumCoins[]; //assume we have 10 coins of each denomination
extern int ItemPrice[ ]; //price in cents
extern int NumItems[ ];
extern double Total_Price;
extern double Item_Total;
class VendingMachine{
public:
void MakeSelection(int NumItems [], int ItemPrice[]);
int ReturnChange(int change, int Coins[], int NumCoins[]);
void ShowMenu();
void DisplayErrorMessage(int error);
void PrintConfidentialInformation(int Denominations, int Items, int Coins[],
int NumCoins[], int ItemPrice[] , int NumItems[]);
private:
int selection;
string code;
double Each_Item[ITEMS]; //price for each item
};
#endif //_VENDING_MACHINE_H_
cpp文件:
#include "Vending Machine.h"
void VendingMachine::MakeSelection(int NumItems [], int ItemPrice[]){
int Denominations = 5;
int const ITEMS = 9;
int Coins[] = {100, 50, 20, 10, 5};
int NumCoins[] = {10, 10, 10, 10, 10}; //assume we have 10 coins of each denomination
int ItemPrice[ ] = { 75, 120, 120, 100, 150, 95, 110, 50, 120 }; //price in cents
int NumItems[ ] = { 10, 10, 10, 10, 10, 10, 10, 10, 10 };
string Product[ITEMS] = {"Water","Coke","Diet Coke","Iced Tea","Swiss Chocolate","Candy",
"Chips","Bubble Gum","Turkish Delight"};
int b = 0;
int a = 1;
cout << "Please enter the number of your choice from the menu above. " << endl;
do{
cout << "\nEnter the number of product OR End transaction: " << endl;
cin >> selection;
cout << "\nYou have selected " <<Product[selection] << endl;
if(selection >= 1 && selection <= 9){
NumItems[selection - 1] = NumItems[selection - 1] - 1;
if(NumItems[selection - 1] >= 0)
Total_Price = Total_Price + ItemPrice[selection - 1];
else{
int error = 1;
DisplayErrorMessage(error); //Item finised
cout <<selection<< endl;
}
}
else if(selection == 10)
cout << "\nTransaction Ended" << endl;
else if(selection == 99){
cout << "Enter the code to access maintanance: " <<endl;
cin >> code;
while(code != "111"){
int error = 2;
DisplayErrorMessage(error);
cin >> code;
}
cout << endl;
cout << "\t\t\t\t\tSales Report " << endl;
cout << "==================================================== " << endl;
cout << endl;
cout << "Number of each product sold with Income cost: " << endl;
cout << endl;
do{
if(NumItems[b] >= 0){
Each_Item[b] = Each_Item[b] + ItemPrice[b];
cout << NumItems[b] << "" << Product[b] << " sold for the total cost of " <<(10 - NumItems [b]) * Each_Item[b]/ 100 <<endl;
Total_Price = Total_Price + ((10 - NumItems[b]) * Each_Item[b]/100);
}
b++;
}while(a <= ITEMS);
}
else{
int error = 3;
DisplayErrorMessage(error);
}
}while(selection != 10);
}
在这里需要一些帮助我无法识别错误。我试图在头文件中声明全局变量,以便可以在任何 cpp 文件中使用它,但我在顶部显示错误。
这样做是对还是错???
#include "Vending Machine.h"
int main(){
int Num_Items[ITEMS], Item_Price[ITEMS];
VendingMachine ex;
ex.ShowMenu();
ex.MakeSelection(Num_Items, Item_Price);
system("PAUSE");
return 0;
}