I've tried almost everything imaginable (apart from the right thing of course), but still can't see why I'm getting an ambiguous error. I am fairly certain it's something really silly but I just can't see it! My compiler shows warnings with the insertion operators and I know they're both being called but I was told sticking in the old virtual
would help me out there (and it hasn't...), not yet anyway!
#include<iostream>
#include<iomanip>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
template <class T>
T produceReport(string title, T accType, int tableRows)
{
cout << title << endl;
for (int x = 0; x < tableRows; x++)
{
cout << "-";
}
cout << endl << accType;
};
class BankAccount
{
private:
int accNum;
double accBal;
public:
BankAccount(int = 0, double = 0.0);
void enterAccountData();
void displayAccount();
};
BankAccount::BankAccount(int num, double bal)
{
accNum = num;
accBal = bal;
}
void BankAccount::enterAccountData()
{
cout << setprecision(2) << fixed;
const int MIN_ACC = 1000, MAX_ACC = 9999, DEFAULT = 0;
cout << "Enter account number: ";
cin >> accNum;
if (accNum < MIN_ACC || accNum > MAX_ACC)
accNum = DEFAULT;
cout << "Enter account balance: $";
cin >> accBal;
}
void BankAccount::displayAccount()
{
cout << "Account#" << accNum <<
", Balance: $" << accBal << endl;
}
class SavingsAccount: virtual public BankAccount
{
friend ostream& operator<<(ostream&, SavingsAccount);
protected:
double intRate;
public:
SavingsAccount(double = 0.0);
void getSavAccount();
void displayAccount();
};
SavingsAccount::SavingsAccount(double rate)
{
intRate = rate;
}
void SavingsAccount::getSavAccount()
{
cout << "Enter interest rate: ";
cin >> intRate;
}
ostream& operator<<(ostream& out, SavingsAccount savAcc)
{
savAcc.displayAccount();
return out;
}
void SavingsAccount::displayAccount()
{
BankAccount::displayAccount();
cout << "Interest rate is: " << intRate << endl;
}
class CheckingAccount: virtual public BankAccount
{
friend ostream& operator<<(ostream&, CheckingAccount);
private:
double monthFee;
int numChecks;
public:
CheckingAccount(int = 0, double = 0.0, double = 0.0, int = 0);
void getCheckAccount();
void displayAccount();
};
CheckingAccount::CheckingAccount(int num, double bal, double fee, int check):
BankAccount(num, bal), monthFee(fee), numChecks(check)
{}
void CheckingAccount::getCheckAccount()
{
cout << "Enter monthly fee for account: $";
cin >> monthFee;
cout << "Enter number of checks remaining: ";
cin >> numChecks;
}
ostream& operator<<(ostream& out, CheckingAccount checkAcc)
{
checkAcc.displayAccount();
return out;
}
void CheckingAccount::displayAccount()
{
BankAccount::displayAccount();
cout << "Monthly fee on account is: $" << monthFee << endl;
cout << "Checks remaining for account: " << numChecks << endl << endl;
}
class CheckingAccountWithInterest: public SavingsAccount, public CheckingAccount
{
public:
CheckingAccountWithInterest();
void displayAccount();
};
CheckingAccountWithInterest::CheckingAccountWithInterest():
CheckingAccount(), SavingsAccount()
{}
void CheckingAccountWithInterest::displayAccount()
{
BankAccount::displayAccount();
intRate = 0.02;
SavingsAccount::displayAccount();
CheckingAccount::displayAccount();
}
int main()
{
const int NUM_ACCS = 5;
unsigned count;
vector<SavingsAccount> savAcc;
SavingsAccount aSavAcc;
vector<CheckingAccount> checkAcc;
CheckingAccount aCheckAcc;
vector<CheckingAccountWithInterest> checkAccWithInt;
CheckingAccountWithInterest aCheckAccWithInt;
for (count = 0; count < NUM_ACCS; count++)
{
aSavAcc.enterAccountData();
aSavAcc.getSavAccount();
savAcc.push_back(aSavAcc);
}
for (count = 0; count < NUM_ACCS; count++)
{
aCheckAcc.enterAccountData();
aCheckAcc.getCheckAccount();
checkAcc.push_back(aCheckAcc);
}
for (count = 0; count < NUM_ACCS; count++)
{
aCheckAccWithInt.enterAccountData();
aCheckAccWithInt.getSavAccount();
aCheckAccWithInt.getCheckAccount();
checkAccWithInt.push_back(aCheckAccWithInt);
}
cout << endl;
for (count = 0; count < NUM_ACCS; count++)
{
produceReport("Savings Account Information", savAcc.at(count), 25);
}
for (count = 0; count < NUM_ACCS; count++)
{
produceReport("Checking Account Information", checkAcc.at(count), 25);
}
for (count = 0; count < NUM_ACCS; count++)
{
produceReport("Checking Account With Interest Information", checkAccWithInt.at(count), 30);
}
}
Error is when calling cout << endl << accType;
template <class T>
T produceReport(string title, T accType, int tableRows)
{
cout << title << endl;
for (int x = 0; x < tableRows; x++)
{
cout << "-";
}
cout << endl << accType;
};
ProduceReport.cpp:16: error: ambiguous overload for 'operator<<' in 'std::cout. std::basic_ostream<_CharT, _Traits>::operator<< [with _CharT = char, _Traits = std::char_traits<char>](std::endl [with _CharT = char, _Traits = std::char_traits<char>]) << accType'
is the error message.
Any help or tips are greatly appreciated for how to get past this error!
CheckingAccountWithInterest
inherits from two classes. Both of them support an operator<<
that is equally likely to be the one that CheckingAccountWithInterest
should use. It's irrelevant that they both call displayAccount()
; the ambiguity occurs before the compiler ever gets there. You need to sort that ambiguity out.