【问题标题】:'std::string' has no member named front [closed]'std :: string'没有名为front的成员[关闭]
【发布时间】:2014-06-20 03:23:01
【问题描述】:
#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;
}

这是我的代码,我使用 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'

我四处搜索并在older question 中发现,如果您的编译器不支持 C++11,就会发生这种情况,但 GCC 从 4.8 开始就应该支持它。难道我做错了什么?我是 C++ 的新手,所以我不会感到惊讶。

注意:我跳过了“main”函数,因为它只用于加载 QT GUI。

【问题讨论】:

  • "GCC 应该支持这一点" - 但前提是你告诉它。您是否在编译器参数中指定了-std=c++11
  • 我没有,也不太清楚该怎么做。
  • 知道了!我必须将“QMAKE_CXXFLAGS += -std=c++11”添加到我的 .pro(qt 项目)文件中:)谢谢!
  • @Cestarian 将CONFIG += c++11 行添加到您的 .pro 文件中。它更好,因为它可以与 Visual Studio 和其他编译器一起使用。
  • @Nazar554 我认为这很有用,您应该将其发布为答案。

标签: string qt function gcc c++11


【解决方案1】:

要使 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-28
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多