C++ to_string 未在范围内声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15569179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:29:27  来源:igfitidea点击:

to_string not declared in scope

c++ubuntuqt-creator

提问by Muhammad Sibghat Khan Oreo

I am trying to make the to_string(NUMBER)function work in my Ubuntu computer for weeks but it never ever works in the QT environment or anywhere else. My code works perfectly on my Mac osx, but when I try running it in Ubuntu it complains that to_stringis not declared in scope. Any solutions to this would be greatly appreciated. I have tried to update the gcc compiler but it didn't fix the problem. Please help.

我试图使该to_string(NUMBER)功能在我的 Ubuntu 计算机上运行数周,但它从未在 QT 环境或其他任何地方运行。我的代码在我的 Mac osx 上完美运行,但是当我尝试在 Ubuntu 中运行它时,它抱怨to_string没有在范围内声明。对此的任何解决方案将不胜感激。我试图更新 gcc 编译器,但它没有解决问题。请帮忙。

I am using QT Creator 4.8.1 and I am using C++ and latest version of Ubuntu.

我使用的是 QT Creator 4.8.1,我使用的是 C++ 和最新版本的 Ubuntu。

int Bint::operator*(int temp){
    Bint b(to_string(temp));
    return ((*this)*b);
}

I was missing the QMAKE_CXXFLAGS += -std=c++0x in the pro file.

我在 pro 文件中缺少 QMAKE_CXXFLAGS += -std=c++0x 。

回答by juanchopanza

There could be different reasons why it doesn't work for you: perhaps you need to qualify the name with std::, or perhaps you do not have C++11 support.

它对您不起作用可能有不同的原因:也许您需要用 限定名称std::,或者您没有 C++11 支持。

This works, provided you have C++11 support:

如果您有 C++11 支持,这有效:

#include <string>

int main()
{
  std::string s = std::to_string(42);
}

To enable C++11 support with g++ or clang, you need to pass the option -std=c++0x. You can also use -std=c++11on the newer versions of those compilers.

要使用 g++ 或 clang 启用 C++11 支持,您需要传递选项-std=c++0x. 您还可以-std=c++11在这些编译器的较新版本上使用。

回答by The Beast

you must compile the file with c++11 support

您必须使用 C++11 支持编译文件

g++ -std=c++0x  -o test example.cpp

回答by MikeC

I fixed this problem by changing the first line in Application.mkfrom

我通过改变第一线固定这一问题Application.mk

APP_STL := gnustl_static

to

APP_STL := c++_static

回答by ashwani nagar

You need to make some changes in the compiler. In Dev C++ Compiler: 1. Go to compiler settings/compiler Options. 2. Click on General Tab 3. Check the checkbox (Add the following commands when calling the compiler. 4. write -std=c++11 5. click Ok

您需要在编译器中进行一些更改。在 Dev C++ 编译器中: 1. 转到编译器设置/编译器选项。2. 点击General Tab 3. 勾选复选框(调用编译器时添加以下命令。4. write -std=c++11 5. 点击Ok