gpt4 book ai didi

c++ - 模棱两可的错误 : template C++

转载 作者:行者123 更新时间:2023-11-30 00:40:22 25 4
gpt4 key购买 nike

我已经尝试了几乎所有可以想象到的方法(当然除了正确的方法),但仍然不明白为什么我会收到模棱两可的错误。我相当确定这真的很愚蠢,但我就是看不到!我的编译器显示了插入运算符的警告,我知道它们都被调用了,但我被告知坚持使用旧的 virtual会帮助我(但还没有……),反正现在还没有!

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

调用 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'

是错误信息。

非常感谢任何有关如何克服此错误的帮助或提示!

最佳答案

CheckingAccountWithInterest继承自两个类。他们都支持 operator<<那同样有可能是CheckingAccountWithInterest应该使用。他们都调用displayAccount()是无关紧要的;歧义发生在编译器到达那里之前。您需要解决这种歧义。

关于c++ - 模棱两可的错误 : template C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6239101/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com