2011-09-30 65 views
1

我必须制作一个简单的类,它有几个子类,其中一个子类有它自己的sublcass。继承... C++语法

AccountClass.h

#ifndef Account_h 
#define Account_h 

class Account{ 

public: 
    //Account(){balance = 0.0;}; Here's where the problem was 
    Account(double bal=0.0){ balance = bal;}; 
    double getBal(){return balance;}; 

protected: 
    double balance; 
}; 
#endif 

SavingsAccount.h

#ifndef SavingsAccount_h 
#define SavingsAccount_h 
#include "AccountClass.h" 
class SavingsAccount : public Account{ 

public: 
    //SavingsAccount(); And here 
    SavingsAccount(double bal = 0.0, int pct = 0.0){ balance = bal; rate = pct; } : Account(bal); 
    void compound(){ balance *= rate; }; 
    void withdraw(double amt){balance -= amt;}; 

protected: 
    double rate; 
}; 

#endif 

CheckingAccount.h

#ifndef CheckingAccount_h 
#define CheckingAccount_h 
#include "AccountClass.h" 
class CheckingAccount : public Account{ 

public: 
    //CheckingAccount(); Here also 
    CheckingAccount(double bal = 0.0, double lim = 500.0, double chg = 0.5){ balance = bal; limit = lim; charge = chg;} : Account(bal); 
    void cash_check(double amt){ balance -= amt;}; 

protected: 
    double limit; 
    double charge; 
}; 

#endif 

TimeAccount.h

#ifndef TimeAccount_h 
#define TimeAccount_h 
#include "SavingsAccount.h" 
class TimeAccount : public SavingsAccount{ 

public: 
    //TimeAccount(); ;) 
    TimeAccount(double bal = 0.0, int pct = 5.0){ balance = bal; rate = pct;} : SavingsAccount(bal,pct); 
    void compound(){ funds_avail += (balance * rate); }; 
    void withdraw(double amt){funds_avail -= amt;}; 

protected: 
    double funds_avail; 
}; 

#endif 

我不断收到的错误,默认构造函数的定义,以及不存在的所有变量...

帮助!

编辑:

修正预处理器IFNDEF的

此外,该解决了。谢谢@Coincoin!

+4

发布一些实际的错误消息的逐字可能会帮助(很多人都看惯了他们的话) – hugomg

+1

'SavingsAccount.h'和' CheckingAccount.h'都使用相同的'#ifndef'警卫 - 这应该对每个头都是唯一的。 'AccountClass.h'根本没有。首先解决这个问题。 – Useless

+0

@Useless是的,我发布我的问题后立即看到了。谢谢:D – OghmaOsiris

回答

1

您定义构造函数的方式有多个错误。

  • 他们中有些人没有一个体内定义
  • 的intialisation名单syntaxicaly不是在正确的地方
  • 您尝试通过使用默认值参数定义两个默认构造函数。

初始化列表在签名和正文之间。

像这样:

SavingsAccount(double bal = 0.0, int pct = 0.0) 
:Account(bal) 
{ 
    balance = bal; rate = pct; 
} 

此外,您已经有了一个默认的构造函数你们又用默认值的所有参数的参数列表,这意味着您要定义两个默认构造。您可能需要删除bal参数的默认值或删除默认构造函数,并将完全默认值构造函数用作默认构造函数。

像这样:

Account(){balance = 0.0;}; 
Account(double bal){ balance = bal;} 

Account(double bal=0.0){ balance = bal;} 
+0

就是这样。我的家庭作业表说,把默认以及放入一个参数化的构造函数...删除默认的问题。 – OghmaOsiris

2

您没有定义默认构造函数,但是当您派生新类时,它们将被默认调用。看起来TimeAccount将成为该问题的罪魁祸首,因为它使用SavingsAccount的默认构造函数。但是,它们都应该被定义,而不是仅仅被声明。

1

这些:

SavingsAccount(); 
CheckingAccount(); 
TimeAccount(); 

需要有定义,以便:

SavingsAccount(){}; 
CheckingAccount(){}; 
TimeAccount(){}; 

应该工作

0

如果你对所有构造函数的参数的默认值,编译器如何知道哪个叫当你写:

TimeAccount t; 

您可能会看到“模糊的函数调用”类型错误。另外,为什么pct int在TimeAccount()?默认参数是double,它将该值存储到double类型的字段中。

0

您需要为所有内容编写默认构造函数。另外如果你需要定义拷贝构造函数/析构函数/赋值运算符,请确保你遵守三条规则。

TimeAccount(): 
    bal(0), 
    pct(5.0) 
{ 
} 

TimeAccount(double bal, int pct) 
{ 
    balance = bal; 
    rate = pct; 
}