不断收到" see declaration of 'class "错误

keep getting " see declaration of 'class " error

本文关键字:错误 class of declaration see      更新时间:2023-10-16

我已经写好了我的代码,但是当我尝试运行它时,一直得到以下内容:

1>------ Build started: Project: Project 2, Configuration: Debug Win32 ------
1>  Project 2.cpp
1>c:usersgeenadesktopzaks stuffproject 2project 2project 2account.h(6): error C2011: 'account' : 'class' type redefinition
1>          c:usersgeenadesktopzaks stuffproject 2project 2project 2account.h(6) : see declaration of 'account'
1>c:usersgeenadesktopzaks stuffproject 2project 2project 2checking.h(9): error C2504: 'account' : base class undefined
1>c:usersgeenadesktopzaks stuffproject 2project 2project 2creditcard.h(9): error C2504: 'account' : base class undefined
1>c:usersgeenadesktopzaks stuffproject 2project 2project 2saving.h(9): error C2504: 'account' : base class undefined
1>c:usersgeenadesktopzaks stuffproject 2project 2project 2project 2.cpp(45): error C2039: 'makeDeposit' : is not a member of 'saving'
1>          c:usersgeenadesktopzaks stuffproject 2project 2project 2saving.h(8) : see declaration of 'saving'
1>c:usersgeenadesktopzaks stuffproject 2project 2project 2project 2.cpp(59): error C2039: 'makeDeposit' : is not a member of 'checking'
1>          c:usersgeenadesktopzaks stuffproject 2project 2project 2checking.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 ==========

我不确定这些错误是什么或我做错了什么…

这是我的Main():

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

这是类save .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;
}

知道为什么我一直得到这些错误吗?

您不断得到错误,因为头文件包含了不止一次。这将导致编译器多次尝试编译类。因为类已经被声明了,所以不能再这样做了。要解决这个问题,你需要在头文件中使用头保护符

#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

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

相关文章: