首页 > 开发 > C++ > 正文

C++c重载出错

2017-09-11 21:35:50  来源: 网友分享

我写了个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;

}

编译通过了。
这是为什么阿?

解决方案

是编译器自动转换在作怪。

operator<<的第二个参数可以是string,你写了一个constructor可以把string转换成Book,同时Book也可以作为operator<<的第二个参数(即你写的那个函数),所以编译器不知道是直接调用string的那个版本,还是把string转换成Book然后调用book的版本。

建议把

Book(string isbn)

改成

explicit Book(string isbn)

关闭自动转换