std::string 值在 Qt 5 中损坏 [英] std::string values become corrupted in Qt 5

查看:29
本文介绍了std::string 值在 Qt 5 中损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Qt5 项目中使用 Presage 文本预测平台,但由于 std 不断出现错误::strings 已损坏.

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

class SomeClass : public ParentClass{民众:SomeClass(const std::string& _past_context) : past_context(_past_context) { }std::string get_past_stream() const {返回过去的上下文;}std::string get_future_stream() const { 返回空;}私人的:const std::string&过去上下文;const std::string 空;};

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

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

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

更新 [2016-07-28]:

澄清问题并删除重复标签:该问题仅在使用 Qt5 时发生.使用 g++ 编译一个仅由 SomeClass 和 Presage 上下文调用程序组成的测试用例工作正常.在 Qt5 中使用 STL 时,字符串在用作返回值后会损坏.

解决方案

在缩小将 std::string 作为返回值在 Qt5 中传递的可能性之后,我在这里发现了一个类似的问题:'std::string' 没有名为 front [关闭]

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

I am trying to use the Presage text-prediction platform in a Qt5 project but keep getting errors due to std::strings being corrupted.

I have this class providing string contexts for the predictive system:

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;

};

This context is called inside the Presage code like this:

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

If I send past_context to std::out inside the get_past_stream method, it shows the correct string. If I send the results of get_past_stream in the getToken method, I get corrupted data.

UPDATE [2016-07-28]:

To clarify the question and remove the duplicate tag: the issue only happens when using Qt5. Compiling with g++ a test case consisting only of SomeClass and the Presage context caller works fine. It is when using the STL in Qt5 that the strings get corrupted after being used as return values.

解决方案

After narrowing the possibilities to the passing of std::string as return values in Qt5, I found a similar problem here: 'std::string' has no member named front [closed]

It seems Qt5 must be explicitly configured to use C++11 STL. Thus, adding CONFIG += c++11 solved my issue too.

这篇关于std::string 值在 Qt 5 中损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆