为什么要写一个std :: string给cout导致一个未知的运算符<<错误?

Hel*_*der 10 c++ cout

我尝试从我的一个方法输出返回值时收到错误:

Error: No operator "<<" matches these operands. Operand types are: std::ostream << std::string
Run Code Online (Sandbox Code Playgroud)

Main.cpp的

#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;
};
Run Code Online (Sandbox Code Playgroud)

应该返回字符串的方法:

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

    return ss.str();
}
Run Code Online (Sandbox Code Playgroud)

And*_*rsK 31

#include <string> 不见了.

  • 问题解决了...我今天感觉很愚蠢哈哈。我那里没有它,“cout”工作正常。谢谢,下次我会记住的。 (2认同)