Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.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+;返回QString+;通过使用SWIG将函数转换为PERL函数 我有C++代码(包括Qt),我想用Perl调用这些函数。 我们可以通过使用SWIG来实现,所以我已经实现了接口,并在Perl脚本中完成了使用这些接口所需的所有工作 我在C++中有一个函数,返回一个qStand值,< /p> QString get_string() { return QString("mystring"); }_C++_Qt_Swig_Perl - Fatal编程技术网

无法从c+;返回QString+;通过使用SWIG将函数转换为PERL函数 我有C++代码(包括Qt),我想用Perl调用这些函数。 我们可以通过使用SWIG来实现,所以我已经实现了接口,并在Perl脚本中完成了使用这些接口所需的所有工作 我在C++中有一个函数,返回一个qStand值,< /p> QString get_string() { return QString("mystring"); }

无法从c+;返回QString+;通过使用SWIG将函数转换为PERL函数 我有C++代码(包括Qt),我想用Perl调用这些函数。 我们可以通过使用SWIG来实现,所以我已经实现了接口,并在Perl脚本中完成了使用这些接口所需的所有工作 我在C++中有一个函数,返回一个qStand值,< /p> QString get_string() { return QString("mystring"); },c++,qt,swig,perl,C++,Qt,Swig,Perl,我又编写了一个类,将在perl脚本中使用,其中有一个函数调用这个get_string()函数并返回const char* const char* get_const_string() { QString str = get_string(); **//here I print str and str .toLocal8Bit().constData() //both are printing the text which i shoud get here** return s

我又编写了一个类,将在perl脚本中使用,其中有一个函数调用这个get_string()函数并返回const char*

const char* get_const_string()
{
   QString str = get_string();

 **//here I print str and str .toLocal8Bit().constData()
 //both are printing the text which i shoud get here**

   return str.toLocal8Bit().constData();

   //here I have tried diff combinations also, as 
   // return str.toStdString().c_str();
}
问题是,在get_const_string()函数中,我可以得到我想要的字符串,但当我在perl脚本中调用此函数时,我得到了未定义的值,即空字符串

你知道这里有什么问题吗

我正在使用perl5,Qt4.8.4


提前感谢。

如果不能使用
QString
返回值,也许可以使用
std::string

如果两者都失败了,并且你没有限制,你可以做一些肮脏的把戏:

QString get_string()
{
  static QByteArray arr;
  QString str = getString();
  arr = str.toLocal8Bit();
  return arr.constData();
}
请注意,在应用程序运行之前,
arr
变量将不可用


编辑:找到了一个可能的解决方案,只需使用
std::string

可能问题在于
get\u const\u string()
函数中的
str
变量是本地变量,一旦程序超出作用域就会被销毁?因此,函数返回的指向char的指针指向无效内存。@vahancho所说的绝对正确,但我还想指出,即使
str
不是本地的,
toLocal8bit()
返回的对象也是临时的,因此不能在return语句中使用(
constData()
仅返回其内部)@vahancho和Predelnik:那么,你们有什么建议吗?即使使用int也会遇到同样的问题(还有一个返回int的函数),即使没有收到任何东西,我也尝试返回常量(例如:10)。即使使用int也会遇到同样的问题(还有一个返回int的函数),我已经尝试返回常量(ex:10),即使我没有收到任何东西。所以我认为问题是其他的。