构造函数问题(C ++)

时间:2011-03-18 18:09:11

标签: c++ visual-studio-2008 class constructor

在我的“Portfolio”构造函数中,它认为我的调试器认为我正在尝试将返回类型设置为它。我不明白为什么。

#include <iostream>
#include <iomanip>

using namespace std;

class BankAccount
{
private:
int accountNum;
double accountBal;
double annualIntRate;
public:
BankAccount(int, double balance = 0.0, double rate = 0.0);
BankAccount();
void enterAccountData();
void computeInterest(int);
void displayAccount();
double getBalance();
};
 //implementation section:
BankAccount::BankAccount()
{
accountNum = 0;
accountBal = 0;
annualIntRate = 0;
}
BankAccount::BankAccount(int account, double balance, double rate)
{
const int LOWEST = 1000;
const int HIGHEST = 9999;

if(account < LOWEST || account > HIGHEST)
{
    accountNum = 0;
}

else
{
    accountNum = account;
}

accountBal = balance;
annualIntRate = rate;
}
void BankAccount::enterAccountData()
{
cout << setprecision(2) << fixed;   

cout << "Enter the account number " << endl;
cin >> accountNum;

while(accountNum < 1000)
{
    cout << "Account numbers cannot be negative or less than 1000 " <<
        "Enter a new account number: " << endl;
    cin >> accountNum;
}

cout << "Enter the account balance " << endl;
cin >> accountBal;

while(accountBal < 0)
{
    cout << "Account balances cannot be negative. " <<
        "Enter a new account balance: " << endl;
    cin >> accountBal;
}
}
void BankAccount::computeInterest(int months)
{
const int MONTHS_IN_YEAR = 12;
int years = 0;
double rate = 0; 
int count = 0;

cout << endl << "How many years will account # " << accountNum << " be held for? (1 to 40 years) " << endl;
cin >> years;
months = years * 12;
count = 0;
while(years < 1 || years > 40)
{
    cout << "Term must be from 1 to 40." << endl;
    cin >> years;
}
do
{
 accountBal = accountBal * annualIntRate + accountBal;
 count++;
 if(count == 12)
 {
    cout << endl << "The balance after " << months << " months is " << endl;
 }
}while(count < months);

cout << "$" << accountBal << endl;
}
void BankAccount::displayAccount()
{
    cout << "Account: " << accountNum << endl << "Balance is: " <<
    accountBal << endl << "Interest rate is: " << annualIntRate;
    cout << endl;
}
double BankAccount::getBalance()
{
return accountBal;
}
class Portfolio
{
private:
int clientNum;
BankAccount checkingAccount;
BankAccount savingsAccount;
double stockMarketHld;
double estateHld;
public:
Portfolio(int,int,double,int,double,double,double,double);
void balancePortfolio();
}
Portfolio::Portfolio(int client, int checkNum, double checkBal,
int savingNum, double savingBal, double savingRate, double stock, double estate)
 : checkingAccount(checkNum, checkBal), 
savingsAccount(savingNum, savingBal, savingRate)
{
clientNum = client;
stockMarketHld = stock;
estateHld = estate;
balancePortfolio();
}
void Portfolio::balancePortfolio()
{
cout << "This works, yay ^_^" << endl;
}
int main()
{
BankAccount bankAcct1(3452);
BankAccount bankAcct2(4325, 0, 0.53);
BankAccount bankAcct3(1113, 58392, 0.25);
BankAccount bankAcct4(4444, 86);
bankAcct1.displayAccount();
bankAcct2.displayAccount();
bankAcct3.displayAccount();
bankAcct4.displayAccount();

system("pause");
return 0;
}

它发生在这里,如果我拿出“双重股票和双重房地产”它可以工作....

 Portfolio::Portfolio(int client, int checkNum, double checkBal,
int savingNum, double savingBal, double savingRate, double stock, double estate)
 : checkingAccount(checkNum, checkBal), 
savingsAccount(savingNum, savingBal, savingRate)
{

任何人都可以为我解释一些事情吗?

2 个答案:

答案 0 :(得分:6)

class Portfolio { ... };结尾处缺少分号。然后,编译器会对文件中的下一个内容感到困惑。

答案 1 :(得分:1)

除了忘记将半色;放在Portfolio类定义的末尾之外,一切看起来都很好:

class Portfolio
{
  private:
    int clientNum;
    BankAccount checkingAccount;
    BankAccount savingsAccount;
    double stockMarketHld;
    double estateHld;
  public:
    Portfolio(int,int,double,int,double,double,double,double);
    void balancePortfolio();
}; //<------------ you forgot this!