14

当我尝试从我的一种方法输出返回值时出现错误:

Error: No operator "<<" matches these operands. Operand types are: std::ostream << std::string

主文件

#include <iostream>
using namespace std;

#include "Book.h"

int main()
{
    book.setTitle("Advanced C++ Programming");
    book.setAuthorName("Linda", "Smith");
    book.setPublisher("Microsoft Press", "One Microsoft Way", "Redmond");
    book.setPrice(49.99);

    cout << book.getBookInfo(); // <-= this won't compile because of the error above.

    int i;
    cin >> i;

    return 0;
};

应该返回字符串的方法:

string Book::getBookInfo()
{
    stringstream ss;
    ss << title << endl << convertDoubleToString(price) << endl;

    return ss.str();
}
4

2 回答 2

37

#include <string>不见了。

于 2012-09-02T20:17:14.427 回答
2

代码是如何得到的定义的string?标头<string>还声明了流插入器。

于 2012-09-02T20:17:36.180 回答