C++ 如何修复“未找到重载成员函数”错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15467115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I fix "Overloaded member function not found" error?
提问by tysowell
I keep getting the following error:
我不断收到以下错误:
"overloaded function not found in 'pizza'"
"overloaded function not found in 'pizza'"
This referring to void outputDescription
and double computePrice
functions, below. I can't figure out what is wrong.
这指的是void outputDescription
和double computePrice
功能,如下。我无法弄清楚出了什么问题。
I am a beginner to C++ but the code looks right. This is for class. I'm supposed to have at least 1 mutator function and 1 accessor function, a function to compute price and a function to output the description of the pizza.
我是 C++ 的初学者,但代码看起来不错。这是上课用的。我应该至少有 1 个 mutator 函数和 1 个访问器函数,一个计算价格的函数和一个输出比萨饼描述的函数。
Here is my code:
这是我的代码:
#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;
}
回答by Andy Prowl
You declare your member outputDescription()
function this way:
您outputDescription()
可以通过以下方式声明您的成员函数:
void outputDescription (string&, string&, int&, int&) const;
// ^^^^^^^ ^^^^^^
But the definition you provide has this signature:
但是您提供的定义具有以下签名:
void pizza::outputDescription (
string type, string size, int pepperoni, int cheese) const
// ^^^^^^ ^^^^^^ ^^^ ^^^ ^^^^^
// REFERENCES ARE MISSING! Qualifier!
You forgot to use the reference in the function definition, and you forgot to add the const
qualifier. The signature used in a member function definition must match the signature of a member function declaration, and yours doesn't. Just make those parameter types referencesto string
and int
, and add the const
qualifier, consistently with the way you declare the function.
您忘记在函数定义中使用引用,并且忘记添加const
限定符。成员函数定义中使用的签名必须与成员函数声明的签名匹配,而您的则不匹配。只需将这些参数类型引用到string
and int
,并添加const
限定符,与您声明函数的方式一致。
Same problem for the computePrice()
member function. Here is how you declare it:
computePrice()
成员函数也有同样的问题。以下是您的声明方式:
double computePrice (string&, int&, int&) const;
// ^^^^^^^ ^^^^ ^^^^
And here is its definition:
这是它的定义:
double pizza::computePrice (string size, int pepperoni, int cheese) const
// ^^^^^^ ^^^ ^^^ ^^^^^
// REFERENCES ARE MISSING! Qualifier!
Of course, the solution is the same.
当然,解决方法是一样的。
回答by spin_eight
Error is due to your methods signatures in declaration (header file) and defenition are different(you forgot &)
错误是由于您在声明(头文件)和defenition 中的方法签名不同(您忘记了&)
回答by Johnsyweb
Turning my comments elsewhere into an answer…
把我在别处的评论变成答案……
The actual solution is to remove all of these arguments (string type, string size, int pepperoni, int cheese
), since they needlessly shadowmember variables (as was pointed out by John in the comments) and should have been pointed out by your compiler!
实际的解决方案是删除所有这些参数 ( string type, string size, int pepperoni, int cheese
),因为它们不必要地隐藏成员变量(正如John 在评论中指出的那样)并且应该由您的编译器指出!
You also need to ensure that your cv-qualifierson the methods are the same for the declarations and the definitions.
您还需要确保方法上的cv 限定符对于声明和定义是相同的。
As such your declarations should be:
因此,您的声明应该是:
void getOrder();
void outputDescription() const;
double computePrice() const;
And the definitions should look like:
定义应如下所示:
void pizza::getOrder()
void pizza::outputDescription() const
double pizza::computePrice() const
This would leave the invocations in main()
looking a lot neater:
这将使调用main()
看起来更整洁:
int main()
{
pizza customerpizza;
customerpizza.getOrder();
customerpizza.outputDescription();
double price = customerpizza.computePrice();
cout << "Total cost is $" << price << ".\n";
}
There are a couple of other things to watch out for, too...
还有一些其他的事情需要注意......
In computePrice()
, you're confusing equality (==
) with assignment (=
). That is if (size = "small")
should be if (size == "small")
and similarly for the other size
s).
在 中computePrice()
,您将等式 ( ==
) 与赋值 ( =
)混淆了。对于其他s来说if (size = "small")
应该是这样)。if (size == "small")
size
In outputDescription()
, the following line is missing something:
在 中outputDescription()
,以下行缺少某些内容:
cout << "You ordered a " << size << << type << " pizza with \n";
// --------------------------------^
// Did you mean to include a space? (' ')?
Learning to read and understand compiler errors (and warnings) is a vital part of learning C++. Keep practising!
学习阅读和理解编译器错误(和警告)是学习 C++ 的重要部分。继续练习!
回答by 0x499602D2
Your method has the signature
你的方法有签名
void outputDescription(string&, string&, int&, int&) const;
but you are defining it as
但你将它定义为
void pizza::outputDescription(string type, string size, int pepperoni, int cheese)
For one, the parameter types do not match and you're not qualifying the latter as a const member function.
一方面,参数类型不匹配,并且您没有将后者限定为 const 成员函数。
Since your methods don't match, the compiler tries to find a suitable overload which if fails to find, hence the error.
由于您的方法不匹配,编译器会尝试找到合适的重载,如果找不到,则会出现错误。
回答by ulyssis2
Your question has been well answered by others, but I would like to point out two other problems with your code.
其他人已经很好地回答了您的问题,但我想指出您的代码的另外两个问题。
Member function
void getOrder (string, string, int, int) ;
should use the references of the variables, otherwise you are not able to set the values of your member values.in member function
double pizza::computePrice
, you should useif (!size.compare("small"))
in stead ofif (size = "small")
.
成员函数
void getOrder (string, string, int, int) ;
应使用变量的引用,否则您无法设置成员值的值。在成员函数中
double pizza::computePrice
,您应该使用if (!size.compare("small"))
代替if (size = "small")
。