Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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++ 调试导致总线错误(内核转储)的模板_C++_Templates - Fatal编程技术网

C++ 调试导致总线错误(内核转储)的模板

C++ 调试导致总线错误(内核转储)的模板,c++,templates,C++,Templates,我有以下代码,其中包含一个名为FromString的模板,该模板接受一个字符串并将其转换为第二个参数的类型: #include <iostream> #include <sstream> using namespace std; template<typename Type_t> inline Type_t FromString(const string& S, Type_t& T) { istringstream sin(S);

我有以下代码,其中包含一个名为FromString的模板,该模板接受一个字符串并将其转换为第二个参数的类型:

#include <iostream>
#include <sstream>

using namespace std;

template<typename Type_t>
inline Type_t FromString(const string& S, Type_t& T)
  {
   istringstream sin(S);
   sin>>T; 
   return T;
  }

int main () {

   string words = "52";
   int number = 0;

   FromString(words, number);

   cout << number << endl;
   return 0;
}
我试着查看两个参数的类型,S和T,但当出现错误时,它们似乎并不异常

您建议我采取什么步骤来确定问题的根源

不确定这是否重要,但我的电脑上有
gcc版本5.4.0
,服务器上有
gcc版本4.8.5

谢谢你的帮助

[编辑] gdb调试程序提供以下输出:

Program received signal SIGBUS, Bus error.
basic_istringstream (__mode=std::_S_in, __str="mystring", this=0x7fffffff8a50, __in_chrg=<optimized out>, __vtt_parm=<optimized out>) at /usr/include/c++/4.8.2/sstream:328
328       : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in)

但是输入字符串似乎是正确的,

传递引用而不是值。那么你就不需要返回
t
的副本了。你的调试器提供了什么见解吗?@StoryTeller Ops my bad,我从一个非工作版本复制了代码,即使有引用,我仍然会得到错误。我编辑了我的question@tadman我将调试器的输出添加为一个编辑器。您应该检查核心转储,查看堆栈跟踪,找出哪个模板失败,使用哪些参数。传递引用而不是值。那么你就不需要返回
t
的副本了。你的调试器提供了什么见解吗?@StoryTeller Ops my bad,我从一个非工作版本复制了代码,即使有引用,我仍然会得到错误。我编辑了我的question@tadman我将调试器的输出添加为edit,您应该检查核心转储,查看堆栈跟踪,找出哪个模板失败,使用哪个参数。
Program received signal SIGBUS, Bus error.
basic_istringstream (__mode=std::_S_in, __str="mystring", this=0x7fffffff8a50, __in_chrg=<optimized out>, __vtt_parm=<optimized out>) at /usr/include/c++/4.8.2/sstream:328
328       : __istream_type(), _M_stringbuf(__str, __mode | ios_base::in)
(gdb) print __str
$7 = "mystring"
(gdb) print this
$8 = (std::basic_istringstream<char, std::char_traits<char>, std::allocator<char> > * const) 0x7fffffff8a50