2011-03-18 134 views
0

在我的「投資組合」構造函數中,它認爲我的調試器認爲我試圖設置返回類型。我不明白爲什麼。構造函數問題(C++)

#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) 
{ 

任何人都可以揭示事物對我來說一些輕?

+1

你缺少你的投資組合的類定義的最後一個分號。可能會混淆你的編譯器。 – 2011-03-18 18:15:18

+0

沒有像這樣的構造。你把這件事和這件事混淆起來; – 2011-03-18 18:19:04

+0

@Thereis:哦,有一個while循環,然後有一個「Do while」循環。 – Alex 2011-03-18 18:23:48

回答

6

您錯過了class Portfolio { ... };末尾的分號。然後編譯器繼續對文件中的下一件事感到困惑。

+0

呃...謝謝我忘記了課程總是需要分號碼,而且這次沒有看到我的錯誤....謝謝。任何其他的指針,你可以給我?以防萬一我的代碼看起來不太好?我將嘗試在某些時候實現#ifndef和#endif。 – Alex 2011-03-18 18:15:10

1

一切都看起來不錯,只是你忘了把semicolor ;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! 
+1

對我來說,是個愚蠢的錯誤:他們爲什麼不能直截了當地說:「在這樣一些事情上」,你忘了一個分號 - 。 – Alex 2011-03-18 18:17:13