Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ std::stringstream“;str";什么也不退?_C++_String_Templates - Fatal编程技术网

C++ std::stringstream“;str";什么也不退?

C++ std::stringstream“;str";什么也不退?,c++,string,templates,C++,String,Templates,我在使用std::stringstream时遇到问题。如下所示,我的函数完全不返回任何内容。即使在模板函数之外尝试stringstream,调用.str()时仍然不会返回任何结果 template < class T > std::string toString( const T &t ) { std::stringstream temp; temp << t; std::cout << temp.str() <<

我在使用
std::stringstream
时遇到问题。如下所示,我的函数完全不返回任何内容。即使在模板函数之外尝试stringstream,调用.str()时仍然不会返回任何结果

template < class T > std::string toString( const T &t )
{
    std::stringstream temp;
    temp << t;
    std::cout << temp.str() << std::endl;
    return temp.str();
}

std::string test = "test" + toString( 1 );
std::cout << test << std::endl;
std::stringstream stream;
stream << "test" << 1;
std::cout << stream.str() << std::endl;
templatestd::string to字符串(const T&T)
{
std::stringstream温度;

temp尝试
ostringstream
而不是
stringstream

==更新==

我使用GCC 4.6.1编译了您的代码,其工作原理如下:

#include <iostream>
#include <sstream>

using namespace std;

template < class T > std::string toString( const T &t )
{
    std::stringstream temp;
    temp << t;
    std::cout << temp.str() << std::endl;
    return temp.str();
}

int main()
{
  std::string test = "test" + toString( 1 );
  std::cout << test << std::endl;
  std::stringstream stream;
  stream << "test" << 1;
  std::cout << stream.str() << std::endl;
  return 0;
}

对于我来说,使用Clang 3.1、GCC 4.4.5和MSVC 10,你的问题就在别处。请在一个新项目中尝试上面的代码,看看问题是否仍然存在。如果没有,@Industrial Anti-Depressant的建议听起来很现实,也就是说,你有一个更好的匹配过载存在。

这不是原因,
stringstream
就像是
ostringstream
istringstream
@Xeo-Oops的组合,你是对的。我试着用
stringstream
编译代码,效果很好(见上文)。了解编译器的类型和OP使用的编译选项会有所帮助。我无法重现您的错误。请显示完整的代码示例来演示它。在此处工作:。问题一定在其他地方…您的工具链是什么?您是否有std::string toString(int t)某个地方?@Industrial的建议听起来很现实。好吧,我现在似乎已经解决了这个问题,从我的预处理器宏中删除了
\u GLIBCXX\u DEBUG
\u GLIBCXX\u DEBUG\u PEDANTIC
。我想我有点含糊不清,我知道代码是有效的。它在我第一次启动项目时就起作用了,但现在我过了一段时间又回来了,它确实有效已经停止工作了,这很奇怪。@jackyyl:请按照我在底部的回答去做,然后向我汇报。
1
test1
test1
#include <iostream>
#include <sstream>
#include <string>

template < class T > std::string toString( const T &t )
{
    std::stringstream temp;
    temp << t;
    std::cout << temp.str() << std::endl;
    return temp.str();
}

int main(){
    std::string test = "test" + toString( 1 );
    std::cout << test << std::endl;
    std::stringstream stream;
    stream << "test" << 1;
    std::cout << stream.str() << std::endl;
}
1
test1
test1