【发布时间】:2013-03-06 05:11:34
【问题描述】:
我不断收到以下错误:
"overloaded function not found in 'pizza'"
这指的是下面的void outputDescription 和double computePrice 函数。我不知道出了什么问题。
我是 C++ 的初学者,但代码看起来不错。这是上课用的。我应该至少有 1 个 mutator 函数和 1 个 accessor 函数,一个计算价格的函数和一个输出比萨饼描述的函数。
这是我的代码:
#include <iostream>
#include <string>
using namespace std;
class pizza
{
public:
void getOrder (string, string, int, int) ;
void outputDescription (string&, string&, int&, int&) const;
double computePrice (string&, int&, int&) const;
private:
string type;
string size;
int pepperoni;
int cheese;
};
int main ()
{
pizza customerpizza;
double price;
string type;
string size;
int pepperoni;
int cheese;
customerpizza.getOrder (type, size, pepperoni, cheese);
customerpizza.outputDescription (type, size, pepperoni, cheese);
price = customerpizza.computePrice (size, pepperoni, cheese);
cout << "Total cost is $" << price << ".\n";
system("PAUSE");
return 0;
}
void pizza::getOrder (string type, string size, int pepperoni, int cheese)
{
int pizzaType;
int pizzaSize;
cout << "Please choose 1 for deep dish, 2 for hand tossed, or 3\n"; cout << " for pan pizza.\n";
cin >> pizzaType;
switch(pizzaType)
{
case 1: type = "deep dish";
break;
case 2: type = "hand tossed";
break;
case 3: type = "pan";
break;
default: cout << "You entered an invalid choice. Please\n";
cout << " enter 1 for deep dish, 2 for hand\n";
cout << " tossed, or 3 for pan pizza.\n";
}
cout << "Please choose 1 for small, 2 for medium, or 3 for\n";
cout << " large pizza.\n";
cin >> pizzaSize;
switch(pizzaSize)
{
case 1: size = "small";
break;
case 2: size = "medium";
break;
case 3: size = "large";
break;
default: cout << "You entered an invalid choice. Please\n";
cout << " enter 1 for small, 2 for medium, or\n";
cout << " 3 for large pizza.\n";
}
cout << "How many pepperoni servings on this pizza?\n";
cin >> pepperoni;
cout << "How many cheese servings on this pizza?\n";
cin >> cheese;
}
void pizza::outputDescription (string type, string size, int pepperoni, int cheese)
{
cout << "You ordered a " << size << << type << " pizza with \n";
cout << pepperoni << " servings of pepperoni and "<< cheese << endl;
cout << "servings of cheese.\n";
}
double pizza::computePrice (string size, int pepperoni, int cheese)
{
double price;
if (size = "small")
{
price = 10 + (2 * (pepperoni + cheese));
}
else if (size = "medium")
{
price = 14 + (2 * (pepperoni + cheese));
}
else if (size = "large")
{
price = 17 + (2 * (pepperoni + cheese));
}
return price;
}
【问题讨论】:
-
因为这是针对课堂的,所以您将不得不学习如何正确地进行面向对象。你的
getOrder、outputDescription和computePrice函数应该没有参数。他们应该使用您在pizza中声明的成员变量而不是参数。按照您的编写方式,您可以删除 Pizza 成员变量,并且您的代码将照常工作。 -
如果您可以将来自编译器的完整错误消息作为问题的一部分包含在内,将会很有帮助。
-
谢谢大家。我从来没有使用过这个论坛,因为我认为答案不会及时出现,但是,我很惊讶。你们太棒了,非常乐于助人。不幸的是,直到我发现部分问题后才看到您的回复。我让它工作了,但是约翰,谢谢你对对象函数的评论。我根据您的建议对其进行了重新设计,不发送参数,并且程序运行良好。
-
其实,John,我以为我有它,但是当我再次编译它时,我遇到了麻烦。当我不向函数发送参数时,我会收到消息。错误 C2660:“pizza::getOrder”:函数不接受 0 个参数错误 C2660:“pizza::outputDescription”:函数不接受 0 个参数错误 C2660:“pizza::computePrice”:函数不接受 0 个参数跨度>
-
@tysowell:
function does not take 0 arguments表示您已从调用中删除了参数,但尚未从声明中删除参数。有关详细信息,请参阅下面的答案。
标签: c++ compiler-errors