如何修复"Overloaded member function not found"错误?

How can I fix "Overloaded member function not found" error?

本文关键字:found 错误 not member 何修复 Overloaded function      更新时间:2023-10-16

我一直得到以下错误:

"overloaded function not found in 'pizza'"

指下面的void outputDescriptiondouble 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限定符。成员函数定义中使用的签名必须与成员函数声明的签名相匹配,而您的签名则不然。只需使这些参数类型引用stringint,并添加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++的一个重要部分。继续练习!

你的问题已经被其他人很好地回答了,但是我想指出你的代码中的另外两个问题。

  1. 成员函数void getOrder (string, string, int, int) ;应该使用变量的引用,否则无法设置成员值的值

  2. 在成员函数double pizza::computePrice中,应该使用if (!size.compare("small"))代替if (size = "small")