如何修复"Overloaded member function not found"错误?
How can I fix "Overloaded member function not found" error?
我一直得到以下错误:
"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 3n"; 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. Pleasen";
cout << " enter 1 for deep dish, 2 for handn";
cout << " tossed, or 3 for pan pizza.n";
}
cout << "Please choose 1 for small, 2 for medium, or 3 forn";
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. Pleasen";
cout << " enter 1 for small, 2 for medium, orn";
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;
}
您可以这样声明您的成员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!
当然,解决方法是一样的
错误是由于您的方法在声明(头文件)和定义中的签名不同(您忘记了&)
你的方法有签名
void outputDescription(string&, string&, int&, int&) const;
但是你把它定义为
void pizza::outputDescription(string type, string size, int pepperoni, int cheese)
首先,形参类型不匹配,并且没有将后者限定为const成员函数。
由于你的方法不匹配,编译器试图找到一个合适的重载,但没有找到,因此出现错误
把我在别处的评论变成一个答案…
实际的解决方案是删除所有这些参数(string type, string size, int pepperoni, int cheese
),因为它们不必要地遮蔽成员变量(正如John在评论中指出的那样),并且应该由编译器指出!
您还需要确保方法上的cv-限定符对于声明和定义是相同的。
因此你的声明应该是:
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++的一个重要部分。继续练习! 你的问题已经被其他人很好地回答了,但是我想指出你的代码中的另外两个问题。
-
成员函数
void getOrder (string, string, int, int) ;
应该使用变量的引用,否则无法设置成员值的值 -
在成员函数
double pizza::computePrice
中,应该使用if (!size.compare("small"))
代替if (size = "small")
。
- 为什么我在" 'X' identifier not found' "上收到错误
- "Entry Point Not Found" Azure 中的错误 LightGBM R 包
- 为什么即使我定义了它们"no matching overload found"也会出现错误
- Qt Cmake 错误编译"GuiSupportQt not found"
- 如何在C++的程序中显示Windows的"DLL not found"错误?
- 在DX11应用程序中使用DirectXHelper.h会导致Visual Studio 2015中出现'namespace/class not found'错误
- Visual Studio Express 2012 中的"dll not found"错误
- 编译时"one or more multiply defined symbols found"错误
- 奇怪的"Entry Point Not Found"错误
- R dyn.load "Symbol not found"错误,即使C++代码构建良好
- C++ 类的头文件"symbol(s) not found"错误
- C++函数和'identifier not found'错误
- 尝试执行文件时出现"sh: ./<file> not found"错误
- 在Swift项目中集成Box2d会出现cassert file not found错误
- 如何修复"Overloaded member function not found"错误?
- 无法在 NetBeans 中运行C++:"No Shell Found"错误
- 函数已定义,但仍给出"function not found"错误
- 编译类时出现"Identifier not found"错误
- 扭矩3D建筑模板项目产生"nfd.h file not found"错误是VS2013
- 即使在构建后仍然"Binary Not Found"错误(Eclipse OSX Mtn. Lion)