简体   繁体   English

C ++无法解析标识符to_string

[英]c++ unable to resolve identifier to_string

Good day 美好的一天

I have searched high and low, yet no solution. 我搜索过高低,但没有解决方案。

IDE: NetBeans IDE 8.2 MinGW: Using the "latest" MinGW compiler. IDE: NetBeans IDE 8.2 MinGW:使用“最新的” MinGW编译器。

Original Issue: 原始发行:

I would like to convert an int to string. 我想将int转换为字符串。

Using this method, I attempted to convert to string, however 使用方法,我尝试转换为字符串,但是

this proposed method std::to_string() results in the error when used(with the to_string as the error) 这个提议的方法std::to_string()在使用时导致错误(以to_string为错误)

unable to resolve identifier to_string 无法解析标识符to_string

Project dependancies: 项目相关性:

#include <string>
#include <iostream>

Searching for a solution to this issue, 2 promonent solutions appeared here and here 在寻找解决此问题的方法时, 此处此处出现两个常见的解决方案

Setting the C and C++ compiler versions should apparenlty solve this issue. 设置C和C ++编译器版本应该可以解决此问题。

It did not, any suggestions? 没有,有什么建议吗?

Minimal Code: 最小代码:

#include <string>

struct User_VPN_Info{
    std::string name, expire;
    int DaysLeft;

    User_VPN_Info(){}

    User_VPN_Info(std::string _name, std::string _expire, int _DaysLeft){
        name = _name;
        expire = _expire;
        DaysLeft = _DaysLeft;
    }

    std::string getString(){
        return(name + " + " + expire + " + " + std::to_string(DaysLeft) + " ; ");                                                    
    }                                             //^_______^ problem here
};

It is know issue with MinGW MinGW的已知问题

 gcc -std=gnu++14 -Wall -Wextra main.cpp main.cpp: In function 'int main()': main.cpp:24:26: error: 'to_string' was not declared in this scope cout<<to_string(a)<<endl; gcc --version gcc.exe (GCC) 5.3.0 

Instant of 'standard" MinGw you should use MinGW-w64 - for 32 and 64 bit Windows 即时使用“标准” MinGw,您应该使用MinGW-w64-适用于32位和64位Windows

 /mingw-w64/i686-6.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin ./g++ -std=c++14 main.cpp 

compile without errors 编译没有错误

If you want change toolchain in Netbeans you have to add new Tool Collection in project properties. 如果要更改Netbeans中的工具链,则必须在项目属性中添加新的工具集合。

Before I answer the question, I would like to thank steff for guiding me in the right direction, thus allowing me to solve the problem, please give his answer an upvote if you deem it worthy. 在我回答问题之前,我要感谢steff指导我朝着正确的方向发展,从而使我能够解决问题,如果您认为值得的话,请给他的回答一个评价。

My Installation Info 我的安装信息

Product Version: NetBeans IDE 8.2 (Build 201609300101)
Java: 1.8.0_112; Java HotSpot(TM) 64-Bit Server VM 25.112-b15
Runtime: Java(TM) SE Runtime Environment 1.8.0_112-b15
System: Windows 10 version 10.0 running on amd64; Cp1252; en_ZA (nb)

Setting Up Netbeans, etc 设置Netbeans等

Orignally, I had install mingw32, as specified by the netbeans documentation page for seting up netbeans with c++ , this is to setup the compiler for c++ (since it appears not to do this automatically). 通常,我已经安装了mingw32,这是由netbeans文档页面指定的, 用于使用c ++设置netbeans ,这是为了为c ++设置编译器(因为它似乎没有自动执行此操作)。

I follow all the steps, adding the path to the environment, and not changing any installation paths, to keep it as to the "tutorial" as possible. 我按照所有步骤进行操作,将路径添加到环境中,而不更改任何安装路径,以使其尽可能与“教程”保持一致。

After coding and importing code, I ran into the error mentioned above, and so steff suggested to use the MinGW64 compiler, where I had just used the mingw32 compiler. 在编码和导入代码之后,我遇到了上面提到的错误,因此steff建议使用MinGW64编译器,而我刚刚使用过mingw32编译器。

Solution

Overview of steps taken ( my solution ) 采取的步骤概述( 我的解决方案

  1. Install MinGW64 安装MinGW64
  2. Add toolchain (aka compiler) 添加工具链(又名编译器)
  3. Change project toolchain ( aka compiler) 更改项目工具链(又名编译器)

1. 1。

MinGW64 setup found here from sourceforge MinGW64设置发现这里从SourceForge

Install this, changing no settings ofcourse. 安装此程序,不更改课程设置。

2. 2。

Opening up netbeans > Tools > Options > C/C++ tab. 打开netbeans>工具>选项> C / C ++选项卡。

Here I have MinGW on the left, that is the pre installed compiler, which exists due to the installation instructions from the documentation page (the MinGW32 compiler/toochain which is giving the error) 在这里我的左侧是MinGW,即预安装的编译器,由于文档页面中的安装说明而存在(MinGW32编译器/ toochain给出了错误)。

Click Add (to add new compiler), browse to the location of the newly install MinGW64 compiler, mine is in: 单击添加(添加新的编译器),浏览到新安装的MinGW64编译器的位置,我的位于:

C:\Program Files\mingw-w64\x86_64-6.2.0-win32-seh-rt_v5-rev1\mingw64\

Name the compiler something appropriate, I named mine "MinGW_64" and for the Tool Collection Family, select the "GNU MinGW", and click ok. 将编译器命名为适当的名称,我将其命名为“ MinGW_64”,对于“工具集合系列”,选择“ GNU MinGW”,然后单击“确定”。

  • Now, adding the links to the compiler exe's. 现在,将链接添加到编译器exe。

In my case, I had to fill in 2 sections, namely the C Compiler field and the C++ Compiler field. 就我而言,我必须填写2个部分,即C Compiler字段和C++ Compiler字段。

Please note I am unsure as to which specific exe the c++ fied should point to, I just selected the c++.exe , this compiler solved my problem, I am unsure of the gcc.exe 请注意,我不确定c ++ fied应该指向哪个特定的exe,我只是选择了c++.exe ,此编译器解决了我的问题,我不确定gcc.exe

Please see image below as a reference. 请参见下图作为参考。

在此处输入图片说明

After this is done, make the MinGW64 compiler your default compiler. 完成此操作后,将MinGW64编译器设置为默认编译器。

3. 3。

Finally, if you have an open project in which this error occurs (which I assume is most probably the case), there is one last thing to do. 最后,如果您有一个打开的项目在其中发生此错误(我认为很可能是这种情况),则最后要做的事情是。

Right click on your project > Properties > Build. 右键单击项目>属性>构建。

Look for the option "Tool Collection", the corresponding compiler name should be "MinGW" (same name as that of the compiler I refered to earlier - the faulty one), this should be changed to the new MinGW_64 compiler you have just installed and added in the Tools > Options > C/C++ section. 查找选项“工具集合”,相应的编译器名称应为“ MinGW”(与我之前提到的编译器名称相同-错误的名称),应将其更改为刚安装的新MinGW_64编译器,在“工具”>“选项”>“ C / C ++”部分中添加。

DONE! DONE!

Close your project properties, the project should reparse automatically, if not, right click on project > Code Assistance > Reparse Project, wait for reparse and your problem should be solved. 关闭您的项目属性,该项目应自动重新分析,如果没有,请右键单击项目>代码帮助>重新分析项目,等待重新分析,您的问题得到解决。

I really hope this helps. 我真的希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM