gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 15:35:54 26 4
gpt4 key购买 nike

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

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>丢失了。

关于c++ - 为什么将 std::string 写入 cout 会导致未知运算符 << 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12239415/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com