Expression must have class type

Hi I've had this issue before with another program, but I'm not sure why this is occurring again. I've changed some code around and the error is the same regardless. The error is on line 67 and is "expression must have class type for cup.toString() and cup.size() . Thanks ahead of time. Am I declaring my class incorrectly? I still haven't quite got the hang of classes yet. Also ignore the endl; sections, I meant to switch them to /n, but haven't gotten to it yet.
Thanks ahead of time and hope I gave enough details.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
 #pragma once

#include <iostream>
#include <string>
#include <vector>
using std::string;


class coffeeCup {
private:
    std::string material = "n/a";
    std::string color = "n/a";
    double capacity = 64;
    double contentLevel = 64;
    double quantity = 0;

public:


    coffeeCup cup(std::string mat, std::string col, double cap, double qua = 0) {
        std::cin >> capacity;
        if (capacity <= 64 && capacity > 0) {
            capacity = capacity;
        }
        else {
            std::cout << "Please enter a valid capacity." << std::endl;
            std::cin.ignore();
            std::cin.clear();
        }


    }
    void addCup(std::vector<coffeeCup>& stock) {
        std::string col, mat;
        double cap, qua;
        int ram, sz;
        std::cout << "\nEnter the details of your coffee cup.\n";
        std::cout << "Type of Cup ( Ceramic,  Porcelain, Glass, Stainless Steel, Stoneware, Plastic ";
        std::cin >> mat;
        if (mat == "ceramic" || mat == "Ceramic") {
            material = "Ceramic";
        }
        else if (mat == "Porcelain" || mat == "porcelain") {
            material = "Porcelain";
        }
        else if (mat == "Glass" || mat == "glass") {
            material = "Glass";
        }
        else if (mat == "Stainless steel" || mat == "stainless steel" || mat == "Stainless Steel" || mat == "stainless Steel") {
            material = "Stainless Steel";
        }
        else if (mat == "Stoneware" || mat == "stoneware") {
            material = "Stoneware";
        }
        else if (mat == "Plastic" || mat == "plastic") {
            material = "plastic";
        }

        std::cout << "Enter the color: ";
        std::cin >> col;
        std::cout << "What is the capacity of your coffee cup ( in ounces)?: ";
        std::cin >> cap;
        std::cout << "How much coffee is in the cup ( ounces) : ";
        std::cin >> qua;
        coffeeCup cup(int mat, std::string col, double cap, double qua);
       // stock.push_back(cup);
        std::cout << "Coffee Cup Detail: " << cup.toString() << "\n Number of coffee cups" << cup.size() << std::endl;

    }
    std::string getMaterial(std::string mat) {
        return material;
    }
    string getColor(string col) {
        return color;
    }
    double getCapacity(double cap) {
        return capacity;
    }
    double getfillLevel() {
        return quantity;
    }
    double getContents(double con) {
        return contentLevel;
    }
    void sip(double quantity) {
        quantity = quantity - 0.5;
        std::cout << quantity;
    }
    void pour(double amount) {
        std::cin >> amount;
        if (amount <= 64 && amount >= 0) {
            quantity = quantity - amount;
        }
        else {}
        //overflow
        //underflow
    }
    bool isEmpty() {}
    bool isFull() {}
    double refill(double quantity) {
        double rfill;
        std::cin >> rfill;
        if (rfill > capacity) {
            std::cout << "Please enter an amount of refill that is less than 64 ounces." << std::endl;
            std::cin >> rfill;
        }
        else if (rfill + quantity > capacity) {
            std::cout << "Ouch! You're going to get burnt if you refill that much." << std::endl;
            std::cin >> rfill;
        }
        else if (rfill < 0) {
            std::cout << "You cannot refill  negative amount of coffee. Please enter a valid input." << std::endl;
        }
        else {
            std::cout << "Please enter a valid amount ( a number with no nonnumerical characters." << std::endl;
            //exception handling
        }
    }

    //  void operator==() {}
     // void operator<() {}
     // void operator>() {}
    void operator--() {
        sip(quantity);
    }
    void operator +=(double quantity) {
        refill(quantity);
    }
    void operator-=(double amount) {
        pour(amount);
    }
    void operator<< (std::ostream& os) {
       // os << cup. << "Your cup is";

    }

}; 

Last edited on
 
coffeeCup cup(int mat, std::string col, double cap, double qua);


What are you trying to do here? This doesn't do it. This is a function declaration for a function called cup which returns a type coffeeCup and takes 4 parameters.
@seeplus is right; this doesn't make sense:
1
2
3
4
5
6
7
8
9
10
11
coffeeCup cup (std::string mat, std::string col, double cap, double qua = 0) {
    std::cin >> capacity;
    if (capacity <= 64 && capacity > 0) {
        capacity = capacity;
    }
    else {
        std::cout << "Please enter a valid capacity." << std::endl;
        std::cin.ignore();
        std::cin.clear();
    }
}

You never use the variables "mat," "col," "cap," or "qua."

Try reading this tutorial and see if that helps.
https://www.cplusplus.com/doc/tutorial/classes/

Good luck,
max

Edit:
I edited because I was incorrect in making the assumption that the OP was trying to make a constructor. See below posts.
Last edited on
That's a function definition called cup returning a type coffeeCup with 4 arguments - but it doesn't actually return anything!
It looked to me like he was trying to create a constructor. I did the same thing once with a constructor, and my teacher couldn't figure out what the heck I was trying to do.

Oops. On line 65, he has a call to cup(). You're right, seeplus. I will edit my post (just so he doesn't get confused).

No wait, that's not a call, that's a...function prototype? I'm confused now...

Ok, this is weird.
kg88 wrote:
The error is on line 67 and is "expression must have class type for cup.toString() and cup.size().

@kg88,
So, on line 67, you call cup.toString() and cup.size(). Where are the function definitions for those? Or are you using the <string> library?

https://www.cplusplus.com/reference/string/string/size/?kw=string%3A%3Asize

https://www.cplusplus.com/reference/string/to_string/

Because "cup" is not a string, it's a function. Ok, I'm really confused now...
Last edited on
I'm sorry for the confusion everyone. Also I got caught up in some things and was unable to get back to my computer to respond in a timely manner. Sorry about that too. The assignment is asking me to create a vector with coffee cups where I can select a cup and pour, sip, get the contents, etc for the cups created. I have had some students mention that I should try to put more of my functions in the header files rather than in the main code, so I've been working at doing so, but I'm terrible with vectors ( and c++ in general) and the class I'm taking is moving too fast for me to really understand what I'm actually doing ( just doing the assignments takes me 10+ hours to figure out). At this point, I'm just trying to get the program to somewhat work so that my prof can at least run it.

Thanks for the feedback, it helps so much getting others inputs.

I'm sorry my coding is so terrible, I'm honestly confused with it myself. I like to take my time learning stuff like this and fast paced on this kind of stuff usually turns as poorly for me because I'm combining too many things without really understanding each piece clearly
Hopefully this makes more sense. At least there are no longer issues based on visual basic debugger.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#pragma once

#include <iostream>
#include <string>
#include <vector>
using std::string;


class coffeeCup {
private:
    std::string material = "n/a";
    std::string color = "n/a";
    double capacity = 64;
    double contentLevel = 64;
    double quantity = 0;


public:
    std::string mat = "n/a";
    std::string col = "n/a";
    double cap = 64;
    double qua = 0;
    int size = 0;
   


    /* coffeeCup cup(std::string mat, std::string col, double cap, double qua = 0) {
         std::cin >> capacity;
         if (capacity <= 64 && capacity > 0) {
             capacity = capacity;
         }
         else {
             std::cout << "Please enter a valid capacity." << std::endl;
             std::cin.ignore();
             std::cin.clear();
         }


     }*/
    void addCup(std::vector<coffeeCup>& stock) {
        std::string col, mat;
        double cap, qua;
        std::cout << "\nEnter the details of your coffee cup.\n";
        std::cout << "Type of Cup ( Ceramic,  Porcelain, Glass, Stainless Steel, Stoneware, Plastic ";
        std::cin >> mat;
        if (mat == "ceramic" || mat == "Ceramic") {
            material = "Ceramic";
        }
        else if (mat == "Porcelain" || mat == "porcelain") {
            material = "Porcelain";
        }
        else if (mat == "Glass" || mat == "glass") {
            material = "Glass";
        }
        else if (mat == "Stainless steel" || mat == "stainless steel" || mat == "Stainless Steel" || mat == "stainless Steel") {
            material = "Stainless Steel";
        }
        else if (mat == "Stoneware" || mat == "stoneware") {
            material = "Stoneware";
        }
        else if (mat == "Plastic" || mat == "plastic") {
            material = "plastic";
        }

        std::cout << "Enter the color: ";
        std::cin >> col;
        std::cout << "What is the capacity of your coffee cup ( in ounces)?: ";
        std::cin >> cap;
        std::cout << "How much coffee is in the cup ( ounces) : ";
        std::cin >> qua;
        coffeeCup cup;
        // stock.push_back(cup);
        std::cout << "Coffee Cup Detail: " << cup.material << "/n" << cup.color << "/n" << cup.capacity << "/n" << cup.quantity << "/n";

    }

    std::string getMaterial(std::string mat) {
        return material;
    }
    string getColor(string col) {
        return color;
    }
    double getCapacity(double cap) {
        return capacity;
    }
    double getfillLevel() {
        return quantity;
    }
    double getContents(double con) {
        return contentLevel;
    }
    void sip(double quantity) {
        quantity = quantity - 0.5;
        std::cout << quantity;
    }
    void pour(double amount) {
        std::cin >> amount;
        if (amount <= 64 && amount >= 0) {
            quantity = quantity - amount;
        }
        else {}
        //overflow
        //underflow
    }
    bool isEmpty() {}
    bool isFull() {}
    double refill(double quantity) {
        double rfill;
        std::cin >> rfill;
        if (rfill > capacity) {
            std::cout << "Please enter an amount of refill that is less than 64 ounces." << std::endl;
            std::cin >> rfill;
        }
        else if (rfill + quantity > capacity) {
            std::cout << "Ouch! You're going to get burnt if you refill that much." << std::endl;
            std::cin >> rfill;
        }
        else if (rfill < 0) {
            std::cout << "You cannot refill  negative amount of coffee. Please enter a valid input." << std::endl;
        }
        else {
            std::cout << "Please enter a valid amount ( a number with no nonnumerical characters." << std::endl;
            //exception handling
        }
    }

    //  void operator==() {}
     // void operator<() {}
     // void operator>() {}
    void operator--() {
        sip(quantity);
    }
    void operator +=(double quantity) {
        refill(quantity);
    }
    void operator-=(double amount) {
        pour(amount);
    }
    std::ostream& operator<<(std::ostream& os) {
       // os << cup. << "Your cup is";

    }

}; 
Topic archived. No new replies allowed.