C++11 在 Visual Studio 2017 中可用吗?

2022-01-12 00:00:00 visual-studio-2017 c++ c++11

我目前正在使用 Visual Studio Community 2017.通过查看项目属性中的 C++ 语言标准,它们仅提供 C++14 和 C++17.由于我的代码是使用 C++11 编译器完成的先前分配的代码,因此我无法使用诸如 stoi 之类的函数运行我的代码.我的问题是是否有办法将 C++11 添加到 C++ 的语言标准中?

I am currently using Visual Studio Community 2017. From looking at the C++ Language Standards in the project properties, they only provide C++14 and C++17. Since my code was completed for a previous assignment using a compiler for C++11, I am unable to run my code using functions such as stoi. My question is if there is a way to add C++11 to the language standards for C++?

我正在为 GUI 创建一个 DLL,我的初始化是:

I am creating a DLL for a GUI, my initializations are:

#include <string>
#include "stdafx.h"

using namespace std;

这里我正在创建一个分数类,主要错误在 ifstream 中:

Here I am creating a fraction class, the main errors follow in the ifstream:

istream& operator>>(istream& in, Fraction& f) {

string number;
in >> number;                           //read the number

size_t delimiter = number.find("/");    //find the delimiter in the string "/"

if (delimiter != string::npos) {            //if delimiter is not empty

    int n = stoi(number.substr(0, delimiter));      //set numerator from string to integer before the "/"
    int d = stoi(number.substr(delimiter + 1));     //set denominator from string to integer after the "/"

    if (d == 0) { //if denominator is 0
        throw FractionException("Illegal denominator, cannot divide by zero.");  //illegal argument throw
    }
    else if (n == 0 && d != 0) {    //numerator is 0, then set values as zero fraction
        f.numVal = 0;
        f.denVal = 1;
    }
    else {                      //set the values into the fraction and normalize and reduce fraction to minimum
        f.numVal = n;
        f.denVal = d;

        f.normalizeAndReduce(f.numVal, f.denVal);
    }
}
else {  //else if there is no delimiter it would be a single integer
    f.numVal = stoi(number);
    f.denVal = 1;
}

return in;
}

我收到以下错误:

C2679: binary '>>': no operator found which takes a right-hand operator of type 'std::string"
C3861: 'stoi' identifier not found

这个方法在eclipse中工作得很好,不知道我做错了什么.

This method worked perfectly fine in eclipse, not sure what I am doing wrong.

推荐答案

Visual C++ 2017 编译器与 C++11/C++14 兼容,但有一些特定例外:

The Visual C++ 2017 compiler is C++11/C++14 compliant with a few specific exceptions:

  • Expression SFINAE 已实现,但是不完整.(现已在 VS 2017 (15.7) 中完成)
  • 由于可变参数宏的一些错误,完整的 C99 预处理器支持受到限制
  • 两阶段名称查找在 VS 2017(15.3 更新)中,但在 不完整 并且仅在使用 /permissive-(现已在 VS 2017 (15.7) 中完成)
  • Expression SFINAE is implemented, but not complete. (Now complete in VS 2017 (15.7))
  • Full C99 preprocessor support is limited due to some bugs with variadic macros
  • Two phase name lookup is in VS 2017 (15.3 update) but is incomplete and only active when using /permissive- (Now complete in VS 2017 (15.7))

编译器不提供特定的 C++11 模式,默认为 C++14,但该标准完全包含 C++11.C++17 支持正在进行中,需要您使用 /std:c++17 或 /std::c++latest 开关.

The compiler does not offer a specific C++11 mode and defaults to C++14, but that standard is fully inclusive of C++11. C++17 support is in progress, and requires you use the /std:c++17 or /std::c++latest switch.

std::stoi 要求您包含适当的标题,特别是 <string>> 您要么忘记包含该标题 - 要么 - 您没有处理使用 namespace 解析(显式为 std:: 或通过 using namespace std;)

std::stoi requires you include the appropriate header, specifically <string>> Either you forgot to include that header -or- you didn't deal with the namespace resolution (either explicitly as std:: or via using namespace std;)

参见 VS 2017 15.3 中的 C++17 功能和 STL 修复 了解截至 VS 2017 的 C++11/C++14/C++17 标准一致性的最新状态(15.3更新)

See C++17 Features And STL Fixes In VS 2017 15.3 for the latest status of C++11/C++14/C++17 standards conformance as of the VS 2017 (15.3 update)

更新:有关 Visual C++ 一致性的最新信息,请参阅 Microsoft Docs.

UPDATED: For the latest on Visual C++ conformance, see Microsoft Docs.

既然您已经发布了代码,我发现问题与支持的标准无关.您的问题是您不知道预编译标头如何工作的秘密.

Now that you have posted your code, I see that the problem has nothing to do with which standard is supported. Your problem is that you don't know the secrets of how Precompiled Headers work.

变化:

#include <string>
#include "stdafx.h"

到:

#include "stdafx.h"
#include <string>

-或- 将#include <string> 直接添加到预编译头stdafx.h.

-or- add #include <string> to the precompiled header stdafx.h directly.

请参阅创建预编译头文件

相关文章