【问题标题】:Templated operator creates an error stating that the operator is ambiguous模板化运算符创建一个错误,指出该运算符不明确
【发布时间】:2014-05-11 16:18:21
【问题描述】:

我已经定义了一个模板化的运算符来满足运算符参数中的两种类型的输入。 getDataFromStream() 出现错误,如何定义运算符以消除这种歧义?

BankAccount.h

template <typename T>
istream& operator>>( istream&, T&);     //input operator

template <typename T>
istream& operator>>( istream& is, T& aBankAccount) {
//get BankAccount details from stream
    return ( aBankAccount.getDataFromStream( is));
}

BankAccount.cpp

包括“BankAccount.h”

void BankAccount::readInBankAccountFromFile( const string& fileName) {
    ifstream fromFile;
    fromFile.open( fileName.c_str(), ios::in);  //open file in read mode
    if ( fromFile.fail())
        cout << "\nAN ERROR HAS OCCURED WHEN OPENING THE FILE.";
    else
        fromFile >> (*this);    //read  all info from bank account file
                 ^^
    fromFile.close();           //close file: optional here
}

但也在这个提供错误的函数中 (BankAccount.cpp)

istream& BankAccount::getDataFromStream( istream& is) {
//get BankAccount details from stream
    is >> accountType_;                     //get account type
    is >> accountNumber_;                   //get account number
    is >> sortCode_;                        //get sort code
    is >> creationDate_;                    //get creation date
    is >> balance_;                         //get balance_
    is >> transactions_;                    //get all transactions (if any)
    return is;
}

Cashpoint.cpp #includes "Cashpoint.h"(包含#includes "BankAccount.h")

bool CashPoint::linkedCard( string cashCardFileName) const {
//check that card is linked with account data
    ifstream inFile;
    inFile.open( cashCardFileName.c_str(), ios::in);    //open file
    bool linked(false);
    if ( ! inFile.fail()) //file should exist at this point 
    {   //check that it contain some info in addition to card number
        string temp;
        inFile >> temp; //read card number
        inFile >> temp; //ready first account data or eof
        if (inFile.eof())
            linked = false;
        else
            linked = true;
        inFile.close();         //close file: optional here
    }
    return linked;
}

编辑:操作员的 cashpoint.cpp 和 bankaccount.cpp 中的错误>> 可能与将 BankAccount.h 包含在 Cashpoint.h 中有关

【问题讨论】:

  • 你能发布编译器的确切错误吗?还要发布你创建实例的代码
  • 是否为现金点定义错误? bankaccount.h 包含在 Cashpoint 中,并且错误与它们的 .cpp 有关我是否需要在 Cashpoint 中重载运算符 >> 的定义?

标签: c++ templates operator-overloading


【解决方案1】:

您重载operator&gt;&gt; 的方法对我来说似乎很奇怪。

template <typename T>
istream& operator>>( istream& is, T& aBankAccount) {
//get BankAccount details from stream
    return ( aBankAccount.getDataFromStream( is));
}

你基本上是在说:不管T是什么类型,调用一个方法getDataFromStream就可以了。这没有任何意义!它还会产生模棱两可的重载,因为当Tstd::string 时,编译器现在有两个operator&gt;&gt; 可能调用:一个来自您的代码,一个来自标准库。

您似乎只想为 BankAccount 类型添加重载。

istream& operator>>(istream& is, BankAccount& aBankAccount) {
    return ( aBankAccount.getDataFromStream( is));
}

【讨论】:

  • 函数getDataFromStream是一个虚函数。我需要操作员为 BankAccount 和 CurrentAccount 的实例工作。
  • @matt9251,如果getDataFromStream是一个虚函数,那么上面的代码会做正确的事情。
  • 是的,错误在于它与 cashpoint.cpp 冲突,因为它声明它是模棱两可的“>>”。 cashpoint.cpp 中需要bankaccount.h 并且包含在我很确定会提供冲突的标题中。对于linkedCard 方法,我需要在那里定义一个合适的运算符,但目前我的尝试提供了相同的错误,所以我有点困惑
  • @matt9251,您尝试过我提出的解决方案了吗?
  • 将 BankAccount& 作为参数的解决方案可以正常工作。这就是我在处理 CurrentAccount 实例之前所拥有的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-09
相关资源
最近更新 更多