Class inheritance compile error
-
Could someone point out why there is an error here? Thanks.
class BankAccount { decimal m_ID; public BankAccount(string ID) { m_ID = 0; } } class SavingsAccount : BankAccount { decimal m_ID; //***Compile Error at the following line: No overload for method 'BankAccount' takes '0' arguments public SavingsAccount(string ID) { m_ID = 0; } }
-
Could someone point out why there is an error here? Thanks.
class BankAccount { decimal m_ID; public BankAccount(string ID) { m_ID = 0; } } class SavingsAccount : BankAccount { decimal m_ID; //***Compile Error at the following line: No overload for method 'BankAccount' takes '0' arguments public SavingsAccount(string ID) { m_ID = 0; } }
Since the
SavingsAccount
ctor doesn't have an explicit call to the base class ctor, the compiler will insert a call to the default ctor (the one with no parameters). But there is no such ctor, so you get the error. You need to add the call to the initializer list:public SavingsAccount(string ID) : base(ID) { ... }
--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
-
Since the
SavingsAccount
ctor doesn't have an explicit call to the base class ctor, the compiler will insert a call to the default ctor (the one with no parameters). But there is no such ctor, so you get the error. You need to add the call to the initializer list:public SavingsAccount(string ID) : base(ID) { ... }
--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ