Passing object ref as constructor param

Hi, I was working on a project today and ran into a strange issue. I was trying to pass a reference to an object of one class (potion) as a parameter of the constructor of another class (customer). I won't elaborate on the 'potion' class - it is not the issue, as it has been passed as a parameter to other functions seemingly without issue. However, when coding the constructor for 'customer' like so:
1
2
3
customer(potion& wtb) {
        //more code
    }

I get the errors as follows:
1
2
3
4
5
6
E:\The Apothecary\customer.h|23|error: no matching function for call to 'potion::potion()'|
E:\The Apothecary\customer.h|23|note: candidates are:|
E:\The Apothecary\potions.h|15|note: potion::potion(std::string)|
E:\The Apothecary\potions.h|15|note:   candidate expects 1 argument, 0 provided|
E:\The Apothecary\potions.h|12|note: potion::potion(const potion&)|
E:\The Apothecary\potions.h|12|note:   candidate expects 1 argument, 0 provided|

as if it expects me to pass parameters to the potion object. This also happens, precisely the same, if I attempt to pass the 'potion' object as a pointer, or as a raw object. The really strange thing is, it does that error literally every time I try to compile 'customers' if the constructor exists at all - even if it never references 'potion' in the constructor. This all seems very strange. Does anyone have any ideas? Thanks.
Which line is line 23 in customer.h?
I think it might be your declaration of the class: You have a 'potion' object declared, however you haven't supplied it with any data in your initializer list for the constructor. Assuming this is the case, your class should look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Customer {
    public:
        // Initialize '_wtb' correctly, otherwise it will try to create 
        // with the default constructor.
        // You don't have a default constructor, so you will get an
        // error if you don't do this.
        Customer(const potion& wtb) : _wtb(wtb) {
            // more code
        }

        // ...

    private:
        potion _wtb;

        // ...
};
Last edited on
Hey, sorry I haven't answered in so long. It appears this website's email notifications aren't working! NT3, I'll try that in a bit. Thanks!
Topic archived. No new replies allowed.