class paramaters

Hello everyone, ive been working on this program for a bit trying to figure out how to properly use classes with paramaters, but with the code that i have below i seem to be getting an error telling me that the line with the prototype with paramaters (line 23) does not match with anything in my class 'Product'.
How can i fix this?

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
#include <iostream>
using namespace std;

class Product {
public:
    Product();
    void read();
    void print() const;
private:
    string name;
    double price;
    int quantity;

};

int main() {
    Product milk;
    milk.print();
    milk.read();
    milk.print();
}

Product::Product(string newName, double newPrice, int newQuantity){
    name = newName;
    price = newPrice;
    quantity = newQuantity;

}

void Product::read(){
    cout << "Enter the name of the product: ";
    cin >> ws;
    getline(cin, name);
    cout << "Enter the price for a " << name << ": ";
    cin >> price;
    if (price < 0) {
        cout << "Error: negative price!\n"
             << "Setting price to 0.\n";
        price = 0;
    }
}

void Product::print() const {
    cout << name << " @ $" << price << endl;
}
closed account (SECMoG1T)
yeah you need the constructor declaration in your class you only have this product();
In your class definition, you only declared a prototype for default constructor(constructor without parameters)
In line 23, you are trying to give a definition for a constructor with parameters but you did not declare such function in your class

To fix, your class definition should have a prototype of a constructor with parameters like:
1
2
3
4
5
6
7
8
9
10
11
12
class Product {
public:
    Product(); // default constructor
    Product(string newName, double newPrice, int newQuantity) // cons with parameters
    void read();
    void print() const;
private:
    string name;
    double price;
    int quantity;

};
Topic archived. No new replies allowed.