gpt4 book ai didi

c++ - C++ 中的 operator<< 和 stringstream 重载

转载 作者:行者123 更新时间:2023-11-30 05:06:14 25 4
gpt4 key购买 nike

<分区>

问题:

来自重载operator<< , cout只会打印第一位:``"Hello guys, ", while the stream is multiple shifts <<“大家好,”<<“我来不及了!” << str << "\n"`.

我的问题是,为什么我只得到流的第一位?

代码:

#include <iostream>
#include <sstream>
#include <string>
class Debug {
private:
std::stringstream stream;
public:
std::string str() const{
return stream.str();
}
std::stringstream& operator<<(std::string &s) {
stream << s;
std::cout << s;
return stream;
}
std::stringstream& operator<<(const char s[]) {
stream << s;
std::cout << s;
return stream;
}
};
std::ostream &operator<<(std::ostream &output, const Debug &d) {
output << d.str();
return output;
}
int main() {
Debug debug;
std::string str("Bad input");
debug << "Hello guys, " << "I can't get here!" << str << "\n";
std::cout <<"\n\n"<< debug;
std::cin.get();
return 0;
}

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