【发布时间】:2014-08-05 06:54:39
【问题描述】:
我有以下课程,
#include <iostream>
using namespace std;
class eventHadler
{
public:
virtual void online(string& str)
{
cout<<" str :"<< str << endl; // accessing str here in this function caused "std::length_error"
}
};
而事件生成器类如下:
class my
{
public:
eventHadler *ptr; // this is initialized in one member function
string localStr;
my()
{
localStr.reserve(1);
ptr = NULL;
}
static void myfun(void* pt);
};
void my::myfun(void* ptMy)
{
my *hdl = static_cast < my* >(ptMy);
hdl->localStr.clear();
hdl->localStr.append("mtfun called");
hdl->ptr->online(hdl->localStr);
}
int main()
{
eventHadler eventobj;
my myObj;
myObj.ptr = &eventobj;
my::myfun((void*)&myObj);
return 0;
}
但是通过调用my::myfun(),将localStr 传递给online() 函数会导致以下问题
terminate called after throwing an instance of 'std::length_error'
what(): basic_string::_S_create
我无法重现上述错误,但它发生在我的开发代码中。
解决此错误的任何指针。
P.S:我无法更改 myfun() 原型。
【问题讨论】:
-
eventHaldler没有名为localStr的成员。 -
@juanchopanza : 更正了代码,请立即检查,对不起!
-
发布一些可以编译的代码。
标签: c++ string event-handling