我写了个Book的类
一开始如下:
class Book {
public:
Book(string name,string author,string pub,string isbn, int remain, int borrowed):_bookName(name),_bookAuthor(author),_bookPub(pub),_bookISBN(isbn),_bookRemain(remain),_bookBorrowed(borrowed){}
//...中间省略
}
ofstream& operator<<(ofstream& out, const Book& book){
out << book._bookName << ',' << book._bookAuthor << ',' << book._bookISBN << ',' << book._bookPub << ',' << book._bookRemain << ',' << book._bookBorrowed << '\n';
return out;
}
一开始这个<<操作符是可以用的。
然后我往里面多加了一个构造函数,代码如下
class Book {
public:
Book(string name,string author,string pub,string isbn, int remain, int borrowed):_bookName(name),_bookAuthor(author),_bookPub(pub),_bookISBN(isbn),_bookRemain(remain),_bookBorrowed(borrowed){}
Book(string isbn){
_bookAuthor = "";
_bookName = "";
_bookISBN = isbn;
_bookPub = "";
_bookRemain = 0;
_bookBorrowed = 0;
}
//..
}
加上一个构造后,就开始报错:
./Book.h:79:10: error: use of overloaded operator '<<' is ambiguous (with operand types 'ofstream' (aka 'basic_ofstream<char>') and 'const string'
(aka 'const basic_string<char>'))
out << book._bookName << ',' << book._bookAuthor << ',' << book._bookISBN << ',' << book._bookPub << ',' << book._bookRemain << ',' << book._bookBorrowed << '\n';
~~~ ^ ~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/basic_string.h:2749:5: note: candidate function [with _CharT = char, _Traits =
std::char_traits<char>, _Alloc = std::allocator<char>]
operator<<(basic_ostream<_CharT, _Traits>& __os,
我google了一下错误,找到了这个帖子http://stackoverflow.com/questions/33...
但是依旧不懂,怎么出现的冲突?我没有用模板什么的。
然后我,尝试放入一个没有参数的构造函数:
Book(){
_bookAuthor = "";
_bookName = "";
_bookISBN = "";
_bookPub = "";
_bookRemain = 0;
_bookBorrowed = 0;
}
编译通过了。
这是为什么阿?