【问题标题】:ambiguous error: template C++模棱两可的错误:模板 C++
【发布时间】:2011-06-04 20:07:51
【问题描述】:

我已经尝试了几乎所有可以想象的方法(当然除了正确的方法),但仍然不明白为什么会出现模棱两可的错误。我相当肯定这真的很愚蠢,但我就是看不到它!我的编译器显示了插入运算符的警告,我知道它们都被调用了,但有人告诉我坚持旧的 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 &lt;&lt; endl &lt;&lt; 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&lt;&lt;' in 'std::cout. std::basic_ostream&lt;_CharT, _Traits&gt;::operator&lt;&lt; [with _CharT = char, _Traits = std::char_traits&lt;char&gt;](std::endl [with _CharT = char, _Traits = std::char_traits&lt;char&gt;]) &lt;&lt; accType'

是错误信息。

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

【问题讨论】:

  • 你不能创建一个产生错误的最小样本吗?实际的错误消息也很有用......

标签: c++ ambiguous function-templates


【解决方案1】:

CheckingAccountWithInterest 继承自两个类。它们都支持operator&lt;&lt;,这同样可能是CheckingAccountWithInterest 应该使用的。他们都打电话给displayAccount()是无关紧要的;歧义发生在编译器到达那里之前。你需要解决这个歧义。

【讨论】:

  • 感谢您的帮助,您是说我应该摆脱其中一名运营商CheckingAccountWithInterest 中添加另一个重载 + 并将两者加在一起?
  • 我在CheckingAccountWithInterest 类中添加了一个简单的operator&lt;&lt;。我希望我几天前才尝试过。非常感谢您的帮助。
猜你喜欢
  • 2015-01-12
  • 1970-01-01
  • 1970-01-01
  • 2011-06-19
  • 1970-01-01
  • 2010-11-13
  • 1970-01-01
  • 1970-01-01
  • 2012-12-10
相关资源
最近更新 更多