为什么下面的代码...
#include <iostream>
#include <sstream>
#include <utility>
int main()
{
std::pair<std::string, std::string> pair1((std::ostringstream().flush() << "hello").str(), (std::ostringstream().flush() << "world").str());
return 0;
}
...生成编译器错误...
>g++ main.cpp
main.cpp: In function ‘int main()’:
main.cpp:7: error: ‘struct std::basic_ostream<char, std::char_traits<char> >’ has no member named ‘str’
main.cpp:7: error: ‘struct std::basic_ostream<char, std::char_traits<char> >’ has no member named ‘str’
...而下面的代码不会产生任何编译器错误?
#include <iostream>
#include <sstream>
#include <utility>
int main()
{
std::ostringstream oss1;
std::ostringstream oss2;
oss1 << "hello";
oss2 << "world";
std::pair<std::string, std::string> pair1(oss1.str(), oss2.str());
return 0;
}
如果有人可以建议如何做,理想情况下,我希望像我在第一个代码块中尝试做的那样,完成我的 std::pair 的“单行”创建和填充。谢谢你。