我是编程新手,所以我不确定问题是什么。我一直在犯的错误在AddTopping(string toppings)
。一个是illegal reference to non-static member PizzaOrder::toppings
。另一个是'initializing' can not convert for " to std::string.
这就是我现在所拥有的:
#include <iostream>
#include <string>
using namespace std;
class PizzaOrder
{
private:
static const int MAX_SIZE=1000;
int size;
string toppings[MAX_SIZE];
int num_toppings;
public:
static const string toppings_offered [4];
static const int DEFAULT_SIZE= 0;
static const int DEFAULT_TOPPINGS=0;
static const double topping_base_cost;
static const double base_price;
PizzaOrder ();
PizzaOrder (int size);
bool SetSize (int size);
int GetSize () {return size;}
static string AddTopping (string topping);
static int AddTopping (int n);
static double GetPrice ();
static string StringizeSize ();
static string GetToppings ();
static void DisplayPizza();
};
const double PizzaOrder :: topping_base_cost= .5;
const double PizzaOrder :: base_price= 5;
const string PizzaOrder ::toppings_offered[4]={"olives","bell peppers","onions","pepperoni"};
int main ()
{
PizzaOrder order;
char pizza_size;
int topping_choice;
short array_size = sizeof(order.toppings_offered)/sizeof(order.toppings_offered[0]);
cout << "Would you like a size [S]small , [M]medium, [L]large pizza or [Q]quit?" <<endl;
while ( pizza_size != 'Q' || pizza_size != 'q')
{
cin >> pizza_size;
if( pizza_size == 'S' || pizza_size == 's')
order.SetSize(0);
if(pizza_size == 'M' || pizza_size == 'm')
order.SetSize(1);
if(pizza_size == 'L' || pizza_size == 'l')
order.SetSize(2);
while (topping_choice !=0)
{
cout << "Current Pizza : " << order.StringizeSize () << order.GetToppings ();
cout <<"Select an item by number. (Enter 0 when done)" << endl;
for (int i=0; i< array_size-1; i++)
{
cout << (i+1) << ". " << order.toppings_offered[i]<< endl;
}
cout << "Selection?";
cin >>topping_choice;
order.AddTopping(topping_choice);
}
}
return 0;
}
PizzaOrder::PizzaOrder()
{
size=DEFAULT_SIZE;
num_toppings=DEFAULT_TOPPINGS;
}
PizzaOrder::PizzaOrder(int size)
{
if(!SetSize(size))
size=DEFAULT_SIZE;
}
bool PizzaOrder::SetSize(int size)
{
if (size != 0 || size !=1 || size !=2)
return false;
this -> size=size;
return true;
}
string PizzaOrder::AddTopping(string topping)
{
string temp_toppings[]={toppings};
short array_size = sizeof(temp_toppings)/sizeof(temp_toppings[0]);
num_toppings+1;
toppings[num_toppings];
for(int k=0; k<array_size-1; k++)
{
toppings[k]= num_toppings[k];
}
toppings[num_toppings]= topping;
}
int PizzaOrder::AddTopping(int n)
{
string temp_toppings[]={toppings};
short array_size = sizeof(temp_toppings)/sizeof(temp_toppings[0]);
num_toppings+1;
toppings[num_toppings];
for(int k=0; k<array_size-1; k++)
{
toppings[k]= num_toppings[k];
}
toppings[num_toppings]=toppings_offered[n];
}
double PizzaOrder ::GetPrice()
{
double price;
double multiplier;
if(size==0)
multiplier=1;
if(size==1)
multiplier=1.15;
if (size==2)
multiplier=1.25;
price= (base_price*multiplier)+(num_toppings*topping_base_cost);
return price;
}
string PizzaOrder::StringizeSize()
{
if(size==0)
return "small";
if(size==1)
return "medium";
if (size==2)
return "large";
}
string PizzaOrder::GetToppings()
{
string temp= "";
for(int x=0,x<num_toppings-1, x++)
temp+= "+ " + toppings[x];
return temp;
}
void PizzaOrder::DisplayPizza()
{
cout << StringizeSize ()<< GetTopping () << GetPrice ();
}
答案 0 :(得分:2)
你有一堆不应该是静态的静态函数,包括AddTopping。阅读静电。
我不完全确定您在“初始化无法转换”错误的哪一行(请在未来的问题中提供完整的错误信息,包括行号),但我打赌这是第一行AddTopping的整数重载。我收集到你正在尝试复制当前“toppings”数组成员的内容(因为你已经将函数声明为static而这是不可访问的 - 这可能是错误消息中缺少类型信息的原因)一个temp_toppings数组。正如你所写的那样,这行不是有效的C ++。我建议你使用谷歌“C ++拷贝阵列”并阅读第一个链接,这是一个很好的阵列教程,位于augustcouncil.com网站上。
答案 1 :(得分:0)
即使你摆脱了所有的错误,你也会在while循环中运行一个无限循环 -
我认为这个循环的目的是打破循环。但它永远不会这样 -
while ( pizza_size != 'Q' || pizza_size != 'q')
{
// .....
}
当pizza_size
等于Q
且||
条件时,条件结果为真且无限循环。它应该是 -
pizza_size ='Q'=&gt;假|| true =&gt;是的 - &gt; while循环继续
pizza_size ='q'=&gt;真|| false =&gt;是的 - &gt; while循环继续
while ( (pizza_size != 'Q') && (pizza_size != 'q'))
{
// .....
}
希望它在以后的阶段有所帮助。