1

I keep getting the following error:

"overloaded function not found in 'pizza'"

This referring to void outputDescription and double computePrice functions, below. I can't figure out what is wrong.

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.

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;
}       
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
tysowell
  • 21
  • 1
  • 1
  • 2
  • 1
    Since this is for class, you're going to have to learn how to do object orientation properly. Your `getOrder`, `outputDescription` and `computePrice` functions should have no arguments. They should be using the member variables you declared in `pizza` not arguments. The way you have written it you could delete the pizza member variables and your code would work just the same. – john Mar 17 '13 at 22:46
  • It would be helpful if you could include the full error message from your compiler as part of your question. – Martin Atkins Mar 17 '13 at 23:04
  • Thanks everyone. I've never used this forum because I assumed answers wouldn't come in a timely manner but, boy am I surprised. You folks are awesome and very helpful. Unfortunately, I didn't see your replies until after I found part of my problem. I got it to work but John, thank you for your comment about object functions. I reworked it with your suggestion to not send arguments and the program works great. – tysowell Mar 18 '13 at 00:04
  • Actually, John, I thought I had it but when compiling it again I've having trouble. When I don't send arguments to the functions I get the message. error C2660: 'pizza::getOrder' : function does not take 0 arguments error C2660: 'pizza::outputDescription' : function does not take 0 arguments error C2660: 'pizza::computePrice' : function does not take 0 arguments – tysowell Mar 18 '13 at 00:17
  • @tysowell: `function does not take 0 arguments` means that you have removed the arguments from the invocation but haven't removed the arguments from the declaration. See my answer below for more details. – johnsyweb Mar 18 '13 at 11:44

5 Answers5

10

You declare your member outputDescription() function this way:

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 references to string and int, and add the const qualifier, consistently with the way you declare the function.

Same problem for the computePrice() member function. Here is how you declare it:

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.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • 1
    Don't forget the `const`-qualifier on the methods. – johnsyweb Mar 17 '13 at 23:22
  • Thanks everyone. I've never used this forum because I assumed answers wouldn't come in a timely manner but, boy am I surprised. You folks are awesome and very helpful. Unfortunately, I didn't see your replies until after I found part of my problem. I got it to work but John, thank you for your comment about object functions. I reworked it with your suggestion to not send arguments and the program works great. Andy, thank you for your answer as well. – tysowell Mar 18 '13 at 00:05
2

Error is due to your methods signatures in declaration (header file) and defenition are different (you forgot &)

spin_eight
  • 3,925
  • 10
  • 39
  • 61
1

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.

Since your methods don't match, the compiler tries to find a suitable overload which if fails to find, hence the error.

David G
  • 94,763
  • 41
  • 167
  • 253
1

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 shadow member variables (as was pointed out by John in the comments) and should have been pointed out by your compiler!

You also need to ensure that your cv-qualifiers on the methods are the same for the declarations and the definitions.

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:

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 sizes).

In outputDescription(), the following line is missing something:

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!

Community
  • 1
  • 1
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
0

Your question has been well answered by others, but I would like to point out two other problems with your code.

  1. 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.

  2. in member function double pizza::computePrice, you should use if (!size.compare("small")) in stead of if (size = "small").

ulyssis2
  • 1,165
  • 3
  • 10
  • 24