猿问

C++c重载出错

我写了个Book的类
一开始如下:classBook{
public:
Book(stringname,stringauthor,stringpub,stringisbn,intremain,intborrowed):_bookName(name),_bookAuthor(author),_bookPub(pub),_bookISBN(isbn),_bookRemain(remain),_bookBorrowed(borrowed){}
//...中间省略
}
ofstream&operator<<(ofstream&out,constBook&book){
out<returnout;
}一开始这个<<操作符是可以用的。
然后我往里面多加了一个构造函数,代码如下classBook{
public:
Book(stringname,stringauthor,stringpub,stringisbn,intremain,intborrowed):_bookName(name),_bookAuthor(author),_bookPub(pub),_bookISBN(isbn),_bookRemain(remain),_bookBorrowed(borrowed){}
Book(stringisbn){
_bookAuthor="";
_bookName="";
_bookISBN=isbn;
_bookPub="";
_bookRemain=0;
_bookBorrowed=0;
}
//..
}加上一个构造后,就开始报错:./Book.h:79:10:error:useofoverloadedoperator'<<'isambiguous(withoperandtypes'ofstream'(aka'basic_ofstream')and'conststring'
(aka'constbasic_string'))
out<~~~^~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:2749:5:note:candidatefunction[with_CharT=char,_Traits=
std::char_traits,_Alloc=std::allocator]
operator<<(basic_ostream<_CharT,_Traits>&__os,我google了一下错误,找到了这个帖子http://stackoverflow.com/questions/33...
但是依旧不懂,怎么出现的冲突?我没有用模板什么的。
然后我,尝试放入一个没有参数的构造函数:Book(){
_bookAuthor="";
_bookName="";
_bookISBN="";
_bookPub="";
_bookRemain=0;
_bookBorrowed=0;
}编译通过了。
这是为什么阿?
慕姐8265434
浏览 344回答 2
2回答

茅侃侃

是编译器自动转换在作怪。operator
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答