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;
}

知道为什么我不断收到这些错误吗?这对我来说毫无意义。

4

2 回答 2

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 文件,然后作为第一个.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
于 2013-04-17T03:39:41.620 回答
0

我认为头文件被包含不止一次。只需#pragma once在每个头文件中添加第一行。

于 2013-04-17T03:44:09.373 回答