Error with function call

Hello everyone!

I'm new to c++ programming and need help compiling one of my programs.
I get the following error messages but I don't know how to fix them:

In constructor 'XNoAccountDefect::XNoAccountDefect(AccountNumber)':|
13|error: no matching function for call to 'XBankingDefect::XBankingDefect()'|
13|note: candidates are:|
06|note: XBankingDefect::XBankingDefect(AccountNumber, std::string)|
06|note: candidate expects 2 arguments, 0 provided|
01|note: XBankingDefect::XBankingDefect(const XBankingDefect&)|
01|note: candidate expects 1 argument, 0 provided|
In constructor 'XNoMoneyDefect::XNoMoneyDefect(AccountNumber)':|
21|error: no matching function for call to 'XBankingDefect::XBankingDefect()'|
21|note: candidates are:|
06|note: XBankingDefect::XBankingDefect(AccountNumber, std::string)|
06|note: candidate expects 2 arguments, 0 provided|
01|note: XBankingDefect::XBankingDefect(const XBankingDefect&)|
01|note: candidate expects 1 argument, 0 provided|
In member function 'Euro Bank::takeMoney(AccountNumber, Euro)':|
88|warning: control reaches end of non-void function [-Wreturn-type]|

I would be more than thankful for any help you can provide!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 class XBankingDefect{
public:
    string message;
    AccountNumber account;

    XBankingDefect(AccountNumber n, string m){message=m;
            account=n;}
    virtual ~XBankingDefect();
};

class XNoAccountDefect : public XBankingDefect{
public:
    XNoAccountDefect(AccountNumber n){message="Error: There is no account '";
            account=n;}
    ~XNoAccountDefect();
};


class XNoMoneyDefect : public XBankingDefect{
public:
    XNoMoneyDefect(AccountNumber n){ message="Error: There is not enough money on account '";
            account=n;}
    ~XNoMoneyDefect();
};
Last edited on
XNoAccountDefect inherits from XBankingDefect. That means that, when you instantiate an object of XNoAccountDefect, the constructor must also call the constructor of XBankingDefect. Since you haven't told XNoAccountDefect::XNoAccountDefect how to pass arguments to XBankingDefect::XBankingDefect, that means it will call the default constructor for XBankingDefect.

But wait! There is no default constructor! Hence the error.

You have defined XBankingDefect such that the constructor needs to take an AccountNumber and a string. So when you define the XNoAccountDefect constructor, you need to specify how those arguments get passed into the XBankingDefect constructor.
This is because you have not specified a constructor to use for XNoAccountDefect or XNoMoneyDefect. You will either need to provide a default initializer within XBankingDefect or use an initializer list (which you should be doing anyway):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class XBankingDefect {
    public:
        string message;
        AccountNumber account;

        XBankingDefect(AccountNumber n, string m)
            : message(m), account(n) {}
        virtual ~XBankingDefect();
};

class XNoAccountDefect : public XBankingDefect {
    public:
        XNoAccountDefect(AccountNumber n)
            : XBankingDefect(n, "Error: There is no account ") {}
        ~XNoAccountDefect();
};

class XNoMoneyDefect : public XBankingDefect {
    public:
        XNoMoneyDefect(AccountNumber n)
            : XBankingDefect(n, "Error: There is not enough money on account ") {}
        ~XNoMoneyDefect();
};


Also, why do you need these subclasses anyway? Just pass the string you want to and be done with it. This just over complicates everything.
Thank you so much, NT3 and MikeyBoy!!!

I used the initializer list and it works fine now!

The subclasses (and the signature of my constructers) were given in my homework excercise specification, so I needed to use them that way.

Another question:
How could I specify how to pass the string and AccountNumber from my subclass constructor to the XBankingDefect constructor, as MikeyBoy suggested?
I think he was just talking about initializer lists, as they are the standard way to do it (and I can't actually think of any other way off the top of my head...).
Okay, thank you very much!

However, I recompiled and fixed another few errors and now I get

"Error: undefined reference to 'vtable for XNoAccountDefect' "

at the initializer list...

Do you know what that means?

I get this undefined reference error also for my destructors although I never use them. They appear for every throw.

EDIT: I figured it out. Forgot the "{}" after my destructor definition. It compiles now.
Thank you very, very much!!!
Last edited on
It probably means that you you haven't put the curly braces after the initializer list. This means that the compiler can't find the constructor. Now, the 'vtable' is the virtual table of functions for the class, used with polymorphic inheritance. It normally puts this with the same source object as the constructor, but if the constructor isn't in any source file (such as if you didn't implement it) then it gives you this error.
Alright, now I also understand why it didn't work before!

Thank you so much for your help and patience, NT3!
I think he was just talking about initializer lists, as they are the standard way to do it (and I can't actually think of any other way off the top of my head...).

Yes, that's exactly what I meant - apologies for being vague!
Topic archived. No new replies allowed.