模板化操作符创建一个错误,指出操作符是二义性的
Templated operator creates an error stating that the operator is ambiguous
我定义了一个模板化的操作符,以满足操作符形参中的两种类型的输入。在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"h1> 是也在这个函数中提供了错误(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"(contains #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.h中包含BankAccount.h有关
标题>您超载operator>>
的方法在我看来很奇怪。
template <typename T>
istream& operator>>( istream& is, T& aBankAccount) {
//get BankAccount details from stream
return ( aBankAccount.getDataFromStream( is));
}
你基本上是在说:不管T
是什么类型,在它上面调用getDataFromStream
方法。这没有任何意义!它也将给出一个模糊的过载,因为编译器现在有两个operator>>
可能调用当T
是std::string
:一个从你的代码和一个从标准库。
看起来你只是想为BankAccount
类型添加一个重载。
istream& operator>>(istream& is, BankAccount& aBankAccount) {
return ( aBankAccount.getDataFromStream( is));
}
相关文章:
- 在重载的全局new操作符中使用静态对象会导致核心转储运行时错误
- 无异常的c++操作符重载错误检查
- Assert()函数抛出一个错误——操作符有问题
- c++操作符string和char*导致歧义错误
- 模板结构中的友元操作符引发重定义错误
- 指针上的操作符=可能导致mac上的分段错误
- 错误:没有找到接受右QUdpSocket的操作符
- 内部编译器错误-开关表达式中的模板化转换操作符
- c++中重载*操作符编译错误
- 重载操作符类型错误
- 赋值操作符重载:错误处理场景
- const_iterator上的间接操作符错误
- 使用重载复合赋值操作符时的运行时错误
- 类型转换操作符重载——Visual studio 2013内部错误
- 派生类操作符=错误
- VS 2015更新3删除拷贝赋值操作符错误
- Boost c++ Phoenix用户定义参数的下标操作符[]错误
- 数字字面值操作符错误
- c++中重载ostream操作符错误
- 我对这个赋值操作符错误的假设是正确的吗?