问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/38622444/stdstring-values-become-corrupted-in-qt-5