class Book {
public:
string title;
string author;
void readBook() {
cout << "Reading" + this->title + " by " + this->author << endl;
}
};
这会导致以下错误。
binary '<<': no operator found which takes a right-hand operand of type 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'
其次
cout << part1 << endl;
这会导致此错误。
Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'std::string'
我的包含语句
#include <string>
#include "pch.h"
#include <iostream>
#include <vector>
#include <exception>
#include <stdio.h>
#include <Windows.h>
using namespace std;
全部在VS 2017中。
我可以得到要打印的字符串
cout << part1.c_str() << part2.c_str() << endl;
有人可以向我解释为什么它不会在没有.c_str()
的情况下打印字符串以及为什么它不接受多个字符串?我正在遵循教程,导师能够毫无错误地处理这些变量。
cout << somethig
由运算符重载实现。
operator <<( const char * )
存在。
但operator <<( string )
不存在。
c_str()
返回const char *
. 因此,您可以将其与cout <<
一起使用
这将起作用..
cout << "Reading" << this->title.c_str() << " by " << this->author.c_str() << endl;