我一直收到以下错误:
"overloaded function not found in 'pizza'"
这指的是void outputDescription
和double computePrice
函数,如下所示。我无法弄清楚出了什么问题。
我是C ++的初学者,但代码看起来很正确。这是为了上课。我应该至少有一个mutator函数和一个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;
}
答案 0 :(得分:8)
您以这种方式声明您的成员outputDescription()
:
void outputDescription (string&, string&, int&, int&) const;
// ^^^^^^^ ^^^^^^
但是你提供的定义有这个签名:
void pizza::outputDescription (
string type, string size, int pepperoni, int cheese) const
// ^^^^^^ ^^^^^^ ^^^ ^^^ ^^^^^
// REFERENCES ARE MISSING! Qualifier!
您忘记在函数定义中使用引用,并且忘记添加const
限定符。成员函数定义中使用的签名必须与成员函数声明的签名匹配,而您的签名则不然。只需将这些参数类型引用设置为string
和int
,然后添加const
限定符,与声明函数的方式一致。
computePrice()
成员函数的问题相同。以下是您的声明方式:
double computePrice (string&, int&, int&) const;
// ^^^^^^^ ^^^^ ^^^^
这是它的定义:
double pizza::computePrice (string size, int pepperoni, int cheese) const
// ^^^^^^ ^^^ ^^^ ^^^^^
// REFERENCES ARE MISSING! Qualifier!
当然,解决方案是一样的。
答案 1 :(得分:2)
错误是由于方法声明中的签名(标题文件)和定义不同(您忘记了&amp;)
答案 2 :(得分:1)
您的方法有签名
void outputDescription(string&, string&, int&, int&) const;
但您将其定义为
void pizza::outputDescription(string type, string size, int pepperoni, int cheese)
首先,参数类型不匹配,并且您没有将后者限定为const成员函数。
由于您的方法不匹配,编译器会尝试找到一个合适的重载,如果找不到,则错误。
答案 3 :(得分:1)
将我的评论转到其他地方......
实际的解决方案是删除所有这些参数(string type, string size, int pepperoni, int cheese
),因为它们是不必要的shadow成员变量(正如John in the comments指出的那样)并且应该被指出你的编译器!
您还需要确保声明和定义的方法cv-qualifiers相同。
因此,您的声明应该是:
void getOrder();
void outputDescription() const;
double computePrice() const;
定义应该如下:
void pizza::getOrder()
void pizza::outputDescription() const
double pizza::computePrice() const
这会使main()
中的调用看起来更整洁:
int main()
{
pizza customerpizza;
customerpizza.getOrder();
customerpizza.outputDescription();
double price = customerpizza.computePrice();
cout << "Total cost is $" << price << ".\n";
}
还有其他一些需要注意的事项......
在computePrice()
中,您将平等( ==
)与作业( =
)混为一谈。 if (size = "small")
应为if (size == "small")
,其他size
也应如此。
在outputDescription()
中,以下行缺少一些内容:
cout << "You ordered a " << size << << type << " pizza with \n";
// --------------------------------^
// Did you mean to include a space? (' ')?
学习阅读和理解编译器错误(和警告)是学习C ++的重要部分。继续练习!
答案 4 :(得分:0)
其他人已经很好地回答了您的问题,但我想指出您的代码还有其他两个问题。
成员函数void getOrder (string, string, int, int) ;
应使用变量的引用,否则您无法设置成员值的值。
double pizza::computePrice
中,您应该使用if (!size.compare("small"))
代替if (size = "small")
。