如何修复"未找到重载的成员函数"错误?

tys*_*ell 1 c++ compiler-errors

我一直收到以下错误:

"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 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;
}       
Run Code Online (Sandbox Code Playgroud)

And*_*owl 8

您以outputDescription()这种方式声明您的成员函数:

void outputDescription (string&, string&, int&, int&) const;
//                      ^^^^^^^  ^^^^^^
Run Code Online (Sandbox Code Playgroud)

但是您提供的定义具有以下特征:

void pizza::outputDescription (
    string type, string size, int pepperoni, int cheese) const
//  ^^^^^^       ^^^^^^       ^^^            ^^^         ^^^^^
//                REFERENCES ARE MISSING!                Qualifier!
Run Code Online (Sandbox Code Playgroud)

您忘记在函数定义中使用引用,并且忘记添加const限定符.成员函数定义中使用的签名必须与成员函数声明的签名匹配,而您的签名则不然.只需将这些参数类型引用stringint,并添加const限定符,与声明函数的方式一致.

computePrice()成员函数的问题相同.以下是您如何声明:

double computePrice (string&, int&, int&) const;
//                   ^^^^^^^  ^^^^  ^^^^
Run Code Online (Sandbox Code Playgroud)

这是它的定义:

double pizza::computePrice (string size, int pepperoni, int cheese) const
//                          ^^^^^^       ^^^            ^^^         ^^^^^
//                              REFERENCES ARE MISSING!             Qualifier!
Run Code Online (Sandbox Code Playgroud)

当然,解决方案是一样的.