std::字符串值在Qt 5中损坏

std::string values become corrupted in Qt 5

本文关键字:损坏 Qt 字符串 std      更新时间:2023-10-16

我正试图在Qt5项目中使用Presage文本预测平台,但由于std::字符串损坏,一直出现错误。

我有一个为预测系统提供字符串上下文的类:

class SomeClass : public ParentClass
{
public:
    SomeClass(const std::string& _past_context) : past_context(_past_context) { }
    std::string get_past_stream() const {
      return past_context;
    }
    std::string get_future_stream() const { return empty; }
private:
    const std::string& past_context;
    const std::string empty;
};

这个上下文在Presage代码中调用如下:

std::string ContextTracker::getToken(const int index) const
{
    std::stringstream pastStringStream(context_tracker_callback->get_past_stream());
    ...
}

如果我在get_past_stream方法内发送past_contextstd::out,它会显示正确的字符串。如果我在getToken方法中发送get_past_stream的结果,我会得到损坏的数据。

更新〔2016-07-28〕:

为了澄清这个问题并删除重复标签:这个问题只在使用Qt5时发生。使用g++编译一个仅由SomeClass和Presage上下文调用程序组成的测试用例效果良好。当在Qt5中使用STL时,字符串在用作返回值后会损坏。

在Qt5中将std::string作为返回值的可能性缩小后,我在这里发现了类似的问题:"std::string"没有名为front[closed]的成员

似乎Qt5必须明确配置为使用C++11 STL。因此,添加CONFIG += c++11也解决了我的问题。