需要一些帮助的两个错误(c++)
Needing some help with two errors (C++)
我一直在编码,这可能是一个容易的错误,我只是忽视了睡眠不足,但问题发生在这段代码。
do
{
aWithIntAcct.enterAccountData();
aWithIntAcct.getSavInfo();
aWithIntAcct.getCheckingInfo();
checkAcct.push_back(aWithIntAcct);
cout << "Would you like to enter another Checking Account with interest? y or n ";
cin >> quitChar;
}while(quitChar != QUIT);
它说我有模棱两可的访问'enterAccountData'(错误C2385)这与未找到的其他(错误C3861)标识符相矛盾。
我的类是继承的,所以我不确定为什么这为我工作。有什么建议吗?
剩余代码:
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
template <class T>
void repeatValue(T val, int times)
{
for(int x = 0; x < times; x++)
{
cout << "-";
}
};
template <class T>
void produceReport(int tableRows, string title, T acctType)
{
cout << tableRows << endl;
cout << title << endl;
cout << acctType;
};
class BankAccount
{
friend ostream& operator<<(ostream&, const BankAccount&);
friend istream& operator>>(istream&, BankAccount&);
private:
int acctNum;
double acctBal;
public:
BankAccount(int = 0,double = 0.0);
double operator+=(BankAccount);
int operator+(BankAccount);
friend bool operator>(BankAccount &acctBalOne, BankAccount &acctBalTwo);
friend bool operator<(BankAccount &acctBalOne, BankAccount &acctBalTwo);
int operator==(const BankAccount acctNumOne);
void enterAccountData();
void displayAccount();
void setAcctNum(int);
void setAcctBal(double);
int getAcctNum();
double getAcctBal();
};
BankAccount::BankAccount(int num, double bal)
{
acctNum = num;
acctBal = bal;
}
double BankAccount::operator+=(BankAccount bankAcct)
{
acctBal += acctBal + bankAcct.acctBal;
return acctBal;
}
int BankAccount::operator+(BankAccount newAcctNum)
{
const int INCREMENT = 1;
newAcctNum.acctNum = acctNum + INCREMENT;
return newAcctNum.acctNum;
}
bool operator>(BankAccount &acctBalOne, BankAccount &acctBalTwo)
{
return acctBalOne.acctBal > acctBalTwo.acctBal;
}
bool operator<(BankAccount &acctBalOne, BankAccount &acctBalTwo)
{
return acctBalOne.acctBal < acctBalTwo.acctBal;
}
int BankAccount::operator== (const BankAccount acctNumOne)
{
int truth = 0;
if(acctNum == acctNumOne.acctNum)
truth = 1;
return truth;
}
ostream& operator<<(ostream& display, const BankAccount& aAcct)
{
display << "Account #" << aAcct.acctNum << endl;
display << "Account Balance $" << aAcct.acctBal << endl;
return display;
}
istream& operator>>(istream& dataIn, BankAccount& aAcct)
{
cout << endl << "Enter in an Account # ";
dataIn >> aAcct.acctNum;
cout << "Enter in an Account Balance $ ";
dataIn >> aAcct.acctBal;
return dataIn;
}
void BankAccount::enterAccountData()
{
cout << "Enter the Account:#";
cin >> acctNum;
cout << endl << "Enter the Account Balance:$";
cin >> acctBal;
}
void BankAccount::displayAccount()
{
cout << "The Account number is #" << acctNum << endl;
cout << "The Account Balance is $" << acctBal << endl;
}
void BankAccount::setAcctNum(int num)
{
acctNum = num;
}
void BankAccount::setAcctBal(double bal)
{
acctBal = bal;
}
int BankAccount::getAcctNum()
{
return acctNum;
}
double BankAccount::getAcctBal()
{
return acctBal;
}
class SavingsAccount : public BankAccount
{
friend ostream& operator<<(ostream&, const SavingsAccount&);
private:
double interest;
public:
SavingsAccount(double = 0.0);
void displaySavAccount();
void getSavInfo();
};
SavingsAccount::SavingsAccount(double intRate)
{
interest = intRate;
}
void SavingsAccount::getSavInfo()
{
cout << "Enter Interest Rate: ";
cin >> interest;
}
ostream& operator<<(ostream& display, SavingsAccount& aSavAcct)
{
aSavAcct.displaySavAccount();
return display;
}
void SavingsAccount::displaySavAccount()
{
cout << " Savings information. " << endl;
cout << "Interest Rate on the account is:" << interest << endl;
}
class CheckingAccount : public BankAccount
{
friend ostream& operator<<(ostream& display, const CheckingAccount& aCheckAcct);
private:
double monthlyFee;
int checksAllowed;
public:
CheckingAccount(double = 0,int = 0,int = 0,double = 0);
void displayCheckAccount();
void getCheckingInfo();
};
CheckingAccount::CheckingAccount(double fee, int allowed, int num, double bal) : BankAccount(num,bal), monthlyFee(fee), checksAllowed(allowed)
{
}
void CheckingAccount::getCheckingInfo()
{
cout << "Enter monthly fee of the Account for the checking account. $";
cin >> monthlyFee;
cout << endl << "How many checks are allowed?" << endl;
cin >> checksAllowed;
}
ostream& operator<<(ostream& display, CheckingAccount& aCheckAcct)
{
aCheckAcct.displayCheckAccount();
return display;
}
void CheckingAccount::displayCheckAccount()
{
cout << " Checking Information " << endl;
BankAccount::displayAccount();
cout << "Monthly fee on the account is:$" << monthlyFee << endl;
cout << "Checks allowed for this account is: " << checksAllowed << endl;
}
class CheckingWithInterest : public SavingsAccount, public CheckingAccount
{
private:
public:
CheckingWithInterest();
void displayWithInterest();
};
CheckingWithInterest::CheckingWithInterest() : CheckingAccount(0,0,9999,0), SavingsAccount(0.03)
{}
void CheckingWithInterest::displayWithInterest()
{
CheckingAccount::displayCheckAccount();
SavingsAccount::displaySavAccount();
}
int main()
{
cout << fixed << setprecision(2);
unsigned count;
vector<SavingsAccount>savAcct;
SavingsAccount aSavAcct;
vector<CheckingAccount>checkAcct;
CheckingAccount aCheckAcct;
vector<CheckingWithInterest>withIntAcct;
CheckingWithInterest aWithIntAcct;
const char QUIT = 'n';
char quitChar = 'y';
cout << "Do you want to enter Savings Information? y or n ";
cin >> quitChar;
do
{
aSavAcct.enterAccountData();
aSavAcct.getSavInfo();
savAcct.push_back(aSavAcct);
cout << "Would you like to enter another Savings Account? y or n ";
cin >> quitChar;
}while(quitChar != QUIT);
cout << "Do you want to enter Checking Information? y or n ";
cin >> quitChar;
do
{
aCheckAcct.enterAccountData();
aCheckAcct.getCheckingInfo();
checkAcct.push_back(aCheckAcct);
cout << "Would you like to enter another Checking Account? y or n ";
cin >> quitChar;
}while(quitChar != QUIT);
cout << "Do you want to enter Checking with interest account Information? y or n ";
cin >> quitChar;
do
{
aWithIntAcct.enterAccountData(); // error points here for both (Line 233)
aWithIntAcct.getSavInfo();
aWithIntAcct.getCheckingInfo();
checkAcct.push_back(aWithIntAcct);
cout << "Would you like to enter another Checking Account with interest? y or n ";
cin >> quitChar;
}while(quitChar != QUIT);
sort(savAcct.begin(), savAcct.end());
for(count = 0; count < savAcct.size(); ++count)
{
repeatValue(savAcct.at(count), count);
cout << endl;
produceReport(savAcct.size(), "Savings Account Information", savAcct.at(count));
}
sort(checkAcct.begin(), checkAcct.end());
for(count = 0; count < checkAcct.size(); ++count)
{
repeatValue(checkAcct.at(count), count);
cout << endl;
produceReport(checkAcct.size(), "Checking Account Information", checkAcct.at(count));
}
sort(withIntAcct.begin(), withIntAcct.end());
for(count = 0; count < withIntAcct.size(); ++count)
{
repeatValue(withIntAcct.at(count), count);
cout << endl;
produceReport(withIntAcct.size(), "Checking with interest Account Information", withIntAcct.at(count));
}
system("pause");
return 0;
}
class BankAccount
class SavingsAccount : public BankAccount
class CheckingAccount : public BankAccount
class CheckingWithInterest : public SavingsAccount, public CheckingAccount
这个继承层次结构(可能)是不正确的。现在它看起来像这样:
BankAccount BankAccount
| |
SavingsAccount CheckingAccount
/
CheckingWithInterest
因此,每个CheckingWithInterest
对象实际上有两个 BankAccount
基类子对象:一个来自它的SavingsAccount
基类子对象,一个来自它的CheckingAccount
基类子对象。
所以,当你试图在CheckingWithInterest
对象上调用BankAccount
成员函数时,例如,
CheckingWithInterest account;
account.enterAccountData();
编译器不知道你是指属于SavingsAccount
基类的BankAccount
中的enterAccountData()
,还是属于CheckingAccount
基类的BankAccount
中的enterAccountData()
。
我不确定什么,确切地说,你的要求是,但如果你只想有一个单一的BankAccount
基类子对象,你可能需要使用虚拟继承当你从BankAccount
派生CheckingAccount
和SavingsAccount
,这样BankAccount
子对象在CheckingWithInterest
中组成时在它们之间共享:
class BankAccount
class SavingsAccount : public virtual BankAccount
class CheckingAccount : public virtual BankAccount
class CheckingWithInterest : public SavingsAccount, public CheckingAccount
如果您确实希望有两个BankAccount
基类子对象,那么当您调用enterAccountData()
时,您需要限定您想要调用成员函数的基类子对象:
account.CheckingAccount::enterAccountData();
相关文章:
- 如何确保在使用基于布尔值的两个方法之一调用方法时避免分支预测错误
- 两个线程一个使用流 Api,另一个线程创建文件失败并出现错误ERROR_SHARING_VIOLATION
- C++哈希表中,两个相同的实现,但一个给出错误
- 使用 SET(C++) 检查两个给定字符串是否是字谜时出现运行时错误
- C++ 分段错误:11 错误,同时编码将两个数字相加的链接列表
- 在 C++ 中创建两个不同的二叉树时出现分段错误
- 交换两个字符串时访问正确的内存时,我遇到了分段错误
- 试图添加两个矢量,但添加顺序错误
- pair的两个构造函数几乎相同,为什么不生成构建错误?
- 纠正C++学校图书馆管理程序中的两个错误
- C 编译时两个错误
- 在主函数c++中使用类时出现两个错误
- 评分程序中的两个错误
- 使用类和标头的两个错误
- 在编译程序之前,我不确定如何修复我的两个错误
- 我正在做一个学校项目,我遇到了两个错误,我不知道如何纠正它们,任何想法
- 我得到两个错误,但我不能识别它们
- 运算符重载 - 知道为什么我在 c++ 程序中遇到这两个错误吗?
- 与使用类有关的两个错误
- 需要一些帮助的两个错误(c++)