0

I'm practicing using basic objects and classes. I know I'm messing something up with syntax but can't quite figure out what.

#include <iostream>
using namespace std;


class Pizza {
    public:

    string topping;
    int slices;


        void GetPizza(string topping, int slices) {
            topping = topping;
            slices = slices;
        }
}; 

int main() {
    Pizza pizza1;

    pizza1.GetPizza("cheese", 8);

    cout << pizza1.GetPizza;

    return 0;
}

I'm trying to print the toppings and number of slices of pizza1. I just keep getting compiler errors. I also wanted to try to print just the topping or just the slice number and couldn't really figure that out either.

EDIT:

v2 of my code with no match for call to errors:

#include <iostream>
using namespace std;


class Pizza {
    public:

    string topping;
    int slices;


        void GetPizza(string t, int s) {
            topping = t;
            slices = s;
        }
}; 

int main() {
    Pizza pizza1;

    pizza1.topping("cheese");

    cout << pizza1.topping("cheese");

    return 0;
}
Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281

2 Answers2

0

pizza1.GetPizza is a class member function. You can't feed it to cout.

If you want to print something, print class member variables. Like this:

cout << pizza1.topping;
cout << pizza1.slices;

Also in the GetPizza function, you have the following lines:

topping = topping;
slices = slices;

How will the compiler know that the left hand side refers to the member variable and the right hand side refers to the function argument (that was probably the intention)? Make sure their names differ.

void GetPizza(string t, int s) {
        topping = t;
        slices = s;
    }

EDIT re v2:

pizza1.topping is not a function. It's a member variable of type string. You can't call it like this:

pizza1.topping("cheese");

If you want to assign a value to it, the right syntax is:

pizza1.topping = "cheese";

Same goes for the next line.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • "Make sure their names differ" - I agree 100%, but there *is* a different option: you can use `this->topping` to unambiguously refer to the member variable. But yes, not shadowing in the first place is *better*. – Jesper Juhl May 10 '19 at 18:05
  • So I changed my variable names like you suggested and tried to print the class variables rather than printing with a call to the function. Now I'm getting "no match for call to ..." errors. I guess if you were trying to write something like this, how would you do it? I get the concept of classes and variables but I'm more fuzzy on what needs to go where and *why* – Shianne May 10 '19 at 18:09
  • I pasted try two into my question – Shianne May 10 '19 at 18:17
  • I don't see any changes. – Seva Alekseyev May 10 '19 at 18:21
  • I tried to edit the post and it said it won't show until it is peer reviewed? Sorry, I'm new to this website. The comment I left on πάντα ῥεῖ's answer sums up my question. – Shianne May 10 '19 at 18:22
  • @SevaAlekseyev _"Make sure their names differ."_ Or use `this->`. – πάντα ῥεῖ May 10 '19 at 18:41
-1

The error is here cout << pizza1.GetPizza; GetPizza method call requires two arguments as per your implementation. Therefore, when printing it you need to pass those arguments as well, similar to what you have done in the below line. pizza1.GetPizza("cheese", 8);

YatShan
  • 425
  • 2
  • 8
  • 22
  • There are more problems as metioned in my answer. – πάντα ῥεῖ May 10 '19 at 18:10
  • @πάντα ῥεῖ I haven't read your answer as it was not there, whilst I wrote my own answer. You must be great to find entire errors in the code, at the same time you must encourage us, who still try to find errors in the code. – YatShan May 10 '19 at 18:21
  • @YatShan I think πάντα ῥεῖ meant that copying stuff from an answer to a comment would not be worth the effort. Instead, that answer was incorporated into the comment "by reference" (pretend it was copied into the comment as an example of a more exhaustive answer). – JaMiT May 10 '19 at 18:26
  • @JaMiT I didn't understand your comment. I haven't copied from anywhere. I wrote my own answer, looking at the question. – YatShan May 10 '19 at 18:28
  • @JaMiT Are you referring copying code lines from the question to my answer? – YatShan May 10 '19 at 18:29
  • @πάντα ῥεῖ Thanks for your comment and feedback, I will improve myself next time. Have a good day. – YatShan May 10 '19 at 18:53
  • 1
    @YatShan Uh... what? OK, I'll try again without pronouns. That answer was incorporated into the comment "by reference" (to understand the initial comment by πάντα ῥεῖ, ***pretend*** that the initial comment by πάντα ῥεῖ started "There are more problems" then was followed by the parts of the answer by πάντα ῥεῖ that describe those problems). – JaMiT May 10 '19 at 21:58