本文介绍了为什么要写一个std :: string到cout导致一个未知的操作符<<错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

应返回字符串的方法:

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

    return ss.str();
}


推荐答案

缺少#include< string>

这篇关于为什么要写一个std :: string到cout导致一个未知的操作符&lt;&lt;错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 18:46