【发布时间】: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