为什么将 std::string 写入 cout 会导致未知运算符<<错误?

Why is writing a std::string to cout causing an unknown operator << error?

本文关键字:lt 未知 运算符 错误 std string 写入 cout 为什么      更新时间:2023-10-16

当我试图从我的一个方法输出返回值时,我遇到了一个错误:

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

主要.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;
};

应该返回字符串的方法:

string Book::getBookInfo()
{
    stringstream ss;
    ss << title << endl << convertDoubleToString(price) << endl;
    return ss.str();
}

#include <string>丢失。

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