不断收到" see declaration of 'class "错误
keep getting " see declaration of 'class " error
我已经写好了我的代码,但是当我尝试运行它时,一直得到以下内容:
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
作为第一行。
相关文章:
- 错误:"Left of getValue must have class/struct/union"
- 当初始值设定项是基类名时'initializer does not name a non-static data member or base class'错误
- unordered_set .find 方法无法编译的 C++ "expression must have class type"错误
- C++错误:"class name"之前的预期类型说明符
- 错误:'class std::unique_ptr<std::set<long unsigned int> >'没有名为 'size' 的成员
- C++ 中的类之间的数据重新循环 - 错误:'<class name>'未在此范围内声明
- 获取生成错误:错误 C2011:'ProfileSettings':'class'类型重定义
- 错误 LNK2001:未解析的外部符号"private: static class Game Game::game_"
- C++编译错误:重新定义'class::class'
- VS2017模板专用化错误无法从'Class *(__cdecl *)(Args...)'转换为'Class *(__cdecl *)(Args...)'
- 在 C++ 中对"Class::function()"错误的未定义引用 [CLion IDE]
- 调用 list.size() 方法给出错误"expression must have class type"
- C++ 中的"this declaration has no storage class or type specifier"错误
- 将数据添加到 CArray 会产生错误"cannot access private member declared in class 'CObject'"
- 如何修复派生类给出错误'declaration has no storage class or type specifier'?
- 为什么我会收到"Expression must have a class type"错误?(C++)
- 让编译器告诉哪些方法不具体,而不是错误'invalid new-expression of abstract class type'
- 共享库中非模板基的模板子类导致未定义的符号类型信息'class'链接错误
- 如何修复编译器错误"class has no member named X"?
- 直接显示过滤器错误"Class is not registered"