2011-06-04 74 views
1

我已经试过几乎所有可以想象的事情(除了正确的事情),但仍然不明白为什么我得到一个模棱两可的错误。我相当肯定这是非常愚蠢的事情,但我看不到它!我的编译器显示插入操作符的警告,我知道它们都被调用,但我被告知坚持在旧的virtual将帮助我在那里(并没有......),还没有!模棱两可的错误:模板C++

#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'

是错误消息时。

任何帮助或提示,非常感谢如何克服这个错误!

+5

你不能创建一个产生错误的最小样本吗?此外,实际的错误信息将是有用的... – ronag 2011-06-04 20:09:36

回答

7

CheckingAccountWithInterest继承自两个类。它们都支持operator<<,这可能是CheckingAccountWithInterest应该使用的那个。他们都称displayAccount()是无关紧要的;在编译器到达那里之前就会出现歧义。你需要排除模糊性。

+0

感谢您的帮助,你是说我应该摆脱其中一个经营者<<?或修改一个,使其看起来不同?或者在'CheckingAccountWithInterest'中添加另一个重载+并将它们加在一起? – Savvy 2011-06-04 20:30:35

+0

最有可能是最后一个。 – 2011-06-04 20:33:49

+0

我在'CheckingAccountWithInterest'类中添加了一个简单的'operator <<'。我希望我几天前才试过这个。十分感谢你的帮助。 – Savvy 2011-06-04 20:45:38