'std :: string'没有名为front的成员

Ces*_*ian 1 string qt gcc function c++11

#include <iostream>
#include <string>
using namespace std;

string buff, resultstr, userstr;

string convert(const string& userstr)
{
    buff = userstr.front();
    resultstr = userstr;
    resultstr.front() = '_';
    resultstr = resultstr + buff + "ay";
    return resultstr;
}
Run Code Online (Sandbox Code Playgroud)

这是我的代码,我使用QT Creator 3.1.0和GCC 4.9.0作为编译器,这给了我:

in function 'std::string convert(const string& userstr)':
'const string' has no member named 'front'
'std::string' has no member named 'front'
Run Code Online (Sandbox Code Playgroud)

我搜索了一下,在一个较旧的问题中发现如果你的编译器不支持C++ 11会发生这种情况,但GCC应该支持自4.8以来.难道我做错了什么?我是C++的新手,所以我不会感到惊讶.

注意:我跳过了"main"函数,因为它仅用于加载QT GUI.

Naz*_*554 5

要使make gcc和其他编译器在C++ 11模式下工作,您应该添加CONFIG += c++11到.pro文件中.然后std::string会有一个成员.front.它比添加更好-std=c++11,QMAKE_CXXFLAGS因为它可以与Qt(Visual Studio,Sun Studio等)支持的任何编译器一起使用.这方面的缺点是你将需要一个Qt 5或更高版本的qmake.qmake然后将从您的编译器mkspec(QMAKE_CXXFLAGS_CXX11)中读取所需的标志并将其追加QMAKE_CXXFLAGS.