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

依然范特西╮ 提交于 2019-12-19 14:04:39

问题


I am getting an error when I try to output the return value from one of my methods:

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

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;
};

Method which should return string:

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

    return ss.str();
}

回答1:


#include <string> is missing.




回答2:


How did the code get the definition of string? The header <string> also declares the stream inserter.



来源:https://stackoverflow.com/questions/12239415/why-is-writing-a-stdstring-to-cout-causing-an-unknown-operator-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!