模糊错误:模板C ++

时间:2011-06-04 20:07:52

标签: c++ ambiguous function-templates

我已经尝试了几乎所有可以想象的东西(当然除了正确的东西),但仍然无法理解为什么我会得到一个模棱两可的错误。我相当肯定这是一件非常愚蠢的事情但我却看不到它!我的编译器显示插入操作符的警告,我知道它们都被调用了但我被告知坚持使用旧的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'

是错误消息。

如何解决此错误,我们非常感谢任何帮助或提示!

1 个答案:

答案 0 :(得分:7)

CheckingAccountWithInterest继承自两个类。它们都支持operator<<同样可能是CheckingAccountWithInterest应该使用的displayAccount()。他们都叫{{1}}并不相关;歧义发生在编译器到达之前。你需要对这种模糊性进行排序。