2013-04-17 42 views
1

我有我寫的代碼,但不斷收到下面當我嘗試並運行它:不斷收到「看到「類的聲明」的錯誤

1>------ Build started: Project: Project 2, Configuration: Debug Win32 ------ 
1> Project 2.cpp 
1>c:\users\geena\desktop\zaks stuff\project 2\project 2\project 2\account.h(6): error C2011: 'account' : 'class' type redefinition 
1>   c:\users\geena\desktop\zaks stuff\project 2\project 2\project 2\account.h(6) : see declaration of 'account' 
1>c:\users\geena\desktop\zaks stuff\project 2\project 2\project 2\checking.h(9): error C2504: 'account' : base class undefined 
1>c:\users\geena\desktop\zaks stuff\project 2\project 2\project 2\creditcard.h(9): error C2504: 'account' : base class undefined 
1>c:\users\geena\desktop\zaks stuff\project 2\project 2\project 2\saving.h(9): error C2504: 'account' : base class undefined 
1>c:\users\geena\desktop\zaks stuff\project 2\project 2\project 2\project 2.cpp(45): error C2039: 'makeDeposit' : is not a member of 'saving' 
1>   c:\users\geena\desktop\zaks stuff\project 2\project 2\project 2\saving.h(8) : see declaration of 'saving' 
1>c:\users\geena\desktop\zaks stuff\project 2\project 2\project 2\project 2.cpp(59): error C2039: 'makeDeposit' : is not a member of 'checking' 
1>   c:\users\geena\desktop\zaks stuff\project 2\project 2\project 2\checking.h(8) : see declaration of 'checking' 
1> Account.cpp 
1> Generating Code... 
1> Skipping... (no relevant changes detected) 
1> Saving.cpp 
1> CreditCard.cpp 
1> Checking.cpp 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

我不知道這些錯誤是或什麼我我做錯了......

這裏是我的主要():

#include "stdafx.h" 
#include <iostream> 
#include <string> 
#include <sstream> 
#include "Account.h" 
#include "Checking.h" 
#include "CreditCard.h" 
#include "Saving.h" 

using namespace std; 

int main() 
{ 
    saving sa; 
    creditCard cca; 
    checking ca; 

    string n; 
    int option; 
    int exit = 1; 
    cout << endl; 
    cout << "Checking Balance:" << " " << "   " << "Savings balance:" << " " << "   " << "Credit Card balance:" << " " << endl; 
    cout << endl; 
    cout << " (1) Savings Deposit " << endl; 
    cout << " (2) Savings withdrawel " << endl; 
    cout << " (3) Checking Deposit " << endl; 
    cout << " (4) Write A Check " << endl; 
    cout << " (5) Credit Card Payment " << endl; 
    cout << " (6) Make A Charge " << endl; 
    cout << " (7) Display Savings " << endl; 
    cout << " (8) Display Checkings " << endl; 
    cout << " (9) Display Credit Card " << endl; 
    cout << " (0) Exit " << endl; 
    cin >> option; 

    do{ 

    switch (option) 

    { 
     case 1 : { 
       double SamtD; 
       cout << " Please enter how much you would like to deposit into savings " << endl; 
       cin >> SamtD; 
       sa.makeDeposit(SamtD); 
       break; 
       } 
     case 2 : { 
       double SamtW; 
       cout << " Please enter how much you would like to withdrawel "<< endl; 
       cin >> SamtW; 
       sa.doWithdraw(SamtW); 
       break; 
       } 
     case 3 : { 
       double CamtD; 
       cout << " Please enter how much you would like to deposit into checkings " << endl; 
       cin >> CamtD; 
       ca.makeDeposit(CamtD); 
       break; 
       } 
     case 4 : { 
       double CamtW; 
       int chkNum; 
       cout << " Please enter how much you wrote on the check " << endl; 
       cin >> CamtW; 
       cout << " Please enter the check number " << endl; 
       cin >> chkNum; 
       ca.writeCheck(chkNum, CamtW); 
       break; 
       } 
     case 5 : { 
       double CCmkP; 
       cout << " Please enter the amount you would like to deposit " << endl; 
       cin >> CCmkP; 
       cca.makePayment(CCmkP); 
       break; 
       } 
     case 6 : { 
       double DoC; 
       string Nm; 
       cout << " Please enter the amount charged to your credit card " << endl; 
       cin >> DoC; 
       cout << " Please enter where the charge was made " << endl; 
       getline(cin, Nm); 
       cca.doCharge(Nm,DoC); 
       break; 
       } 
     case 7 : { 
       sa.display(); 
       break; 
       } 
     case 8 : { 
       ca.display(); 
       break; 
       } 
     case 9 : { 
       cca.display(); 
       break; 
       } 
     case 0 : exit = 0; 
       break; 
     default : exit = 0; 
       cout << " ERROR "; 
    } 
    } 
    while(exit==1); 
    return 0; 
} 

這裏是類saving.h(非常類似於檢查和的信用卡,所以我認爲它們是相關的問題:

#include "stdafx.h" 
#include "iostream" 
#include "Account.h" 
#include <string> 
#include <sstream> 
using namespace std; 

class saving: public account 
{ 
public : 

    double doWithdraw(double amount); 
    string display(); 
    saving(); 
    saving(string itsName, long itsTaxID, double itsBalance); 
}; 

這是保存.cpp文件:

#include "stdafx.h" 
#include "iostream" 
#include "Saving.h" 
#include <string> 
#include <sstream> 
using namespace std; 

saving::saving():account() 
{ 
} 

saving::saving(string itsName, long itsTaxID, double itsBalance): account(itsName, itsTaxID, itsBalance) 
{ 
} 

double saving:: doWithdraw(double amount) 
{ 
    return 0; 
} 

,這裏是基類account.h。節能檢查和的信用卡繼承這個類:

#include "stdafx.h" 
#include "iostream" 
#include <string> 
#include <sstream> 
using namespace std; 
class account { 

public : 
    void setName(string name); void setTaxID(long taxID); void setBalance(double balance); 
    string getName(); long getTaxID(); double getBalance(); 
    double makeDeposit(double amount); 
    account(); 
    account(string itsName, long itsTaxID, double itsBalance); 
    int display(); 

private : 
    string itsName; 
    long itsTaxID; 
    double itsBalance; 

protected : 
    double last10withdraws[10]; 
    double last10deposits[10]; 
    int numdeposits; 
    int numwithdraws; 

}; 

和account.cpp:

#include "stdafx.h" 
#include <iostream> 
#include "Account.h" 
#include <string> 
#include <sstream> 

using namespace std; 

account::account():itsName(""), itsTaxID(0), itsBalance(0) 
{ 
} 

account::account(string itsName, long itsTaxID, double itsBalance): itsName(), itsTaxID(), itsBalance() 
{ 
} 

void account::setName(string name) 
{ 
    itsName = name; 
} 

string account::getName() 
{ 
    return itsName; 
} 

void account::setTaxID(long taxID) 
{ 
    itsTaxID = taxID; 
} 

long account::getTaxID() 
{ 
    return itsTaxID; 
} 

void account::setBalance(double balance) 
{ 
    balance = 100; 
    itsBalance = balance; 
} 

double account::getBalance() 
{ 
    return itsBalance; 
} 

double account::makeDeposit(double amount) 
{ 
    return amount; 
} 

int account::display() 
{ 
    return 0; 
} 

任何想法,爲什麼我不斷收到這些錯誤?這對我來說毫無意義。

+0

'see declaration'行本身不是一個錯誤;這是一個說明,以幫助您解決上一行的錯誤,''類'重新定義' –

回答

2

您不斷收到錯誤,因爲頭文件包含多次。這會導致編譯器嘗試多次編譯類。由於班級已經宣佈你不能再做。爲了解決這個問題,你需要使用頭文件放置在頭文件中。

#ifndef FILENAME_H // <--- first line of your header file. 
#define FILENAME_H 


// Place your header file contents here 


#endif // <--- last line of your header file. 

FILENAME_H典型地匹配頭文件的文件名。

作爲便箋,請從頭文件中刪除#include "stdafx.h"。它只屬於.cpp文件,然後只有作爲第一個#include。例如你的account.h文件看起來像這樣

#ifndef ACCOUNT_H 
#define ACCOUNT_H 

#include "iostream" 
#include <string> 
#include <sstream> 
using namespace std; 
class account { 

public : 
    void setName(string name); void setTaxID(long taxID); void setBalance(double balance); 
    string getName(); long getTaxID(); double getBalance(); 
    double makeDeposit(double amount); 
    account(); 
    account(string itsName, long itsTaxID, double itsBalance); 
    int display(); 

private : 
    string itsName; 
    long itsTaxID; 
    double itsBalance; 

protected : 
    double last10withdraws[10]; 
    double last10deposits[10]; 
    int numdeposits; 
    int numwithdraws; 

}; 

#endif 
+0

我會在我的所有.cpp和.h中使用這些標頭警衛,或者只是我的主? – Dolbyover

+0

在頭文件中。我已經更新了我的答案以澄清這一點。哎呀。 –

+0

謝謝Captain Obvious。我做了這個,現在我得到了我的每個.cpp文件下的這個錯誤....: – Dolbyover

0

我認爲頭文件被多次包含。 只需在每個頭文件中加上#pragma once作爲第一行。

+0

我繼續向所有四個.h文件添加#pragma一次。它擺脫了所有的錯誤,但現在我有四個: – Dolbyover

+0

1>項目2.obj:錯誤LNK2001:無法解析的外部符號「public:class std :: basic_string ,class std: :分配器> __thiscall creditCard :: display(void)「(?display @ creditCard @@ QAE?AV?$ basic_string @ DU?$ char_traits @ D @ std @@ V?$ allocator @ D @ 2 @@ std @@ XZ) – Dolbyover

+0

@ZachKenny你聲明'creditCard :: display()',但沒有提供它的實現。 –